

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return a copy of each string element where all tab characters are replaced by spaces in Numpy
To return a copy of each string element where all tab characters are replaced by spaces, use the numpy.char.expandtabs() method in Python Numpy. We can also set the "tabsize" parameter i.e. replace tabs with tabsize number of spaces. If not given defaults to 8 spaces.
The function expandtabs() returns a copy of each string element where all tab characters are replaced by one or more spaces, depending on the current column and the given tabsize. The column number is reset to zero after each newline occurring in the string. This doesn’t understand other non-printing characters or escape sequences.
The numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_ or numpy.bytes_.
Steps
At first, import the required library −
import numpy as np
Create a One-Dimensional array of string −
arr = np.array(['Bella\tCio', 'Tom\tHanks', 'Monry\tHeist\tSeries'])
Displaying our array −
print("Array...\n",arr)
Get the datatype −
print("\nArray datatype...\n",arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...\n",arr.ndim)
Get the shape of the Array −
print("\nOur Array Shape...\n",arr.shape)
Get the number of elements of the Array −
print("\nElements in the Array...\n",arr.size)
To return a copy of each string element where all tab characters are replaced by spaces, use the numpy.char.expandtabs() method. We can also set the "tabsize" parameter i.e. replace tabs with tabsize number of spaces. If not given defaults to 8 spaces −
print("\nResult (expand tabs)...\n",np.char.expandtabs(arr))
Example
import numpy as np # Create a One-Dimensional array of string arr = np.array(['Bella\tCio', 'Tom\tHanks', 'Monry\tHeist\tSeries']) # Displaying our array print("Array...\n",arr) # Get the datatype print("\nArray datatype...\n",arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...\n",arr.ndim) # Get the shape of the Array print("\nOur Array Shape...\n",arr.shape) # Get the number of elements of the Array print("\nElements in the Array...\n",arr.size) # To return a copy of each string element where all tab characters are replaced by spaces, use the numpy.char.expandtabs() method in Python Numpy # We can also set the "tabsize" parameter i.e. replace tabs with tabsize number of spaces. If not given defaults to 8 spaces. print("\nResult (expand tabs)...\n",np.char.expandtabs(arr))
Output
Array... ['Bella\tCio' 'Tom\tHanks' 'Monry\tHeist\tSeries'] Array datatype... <U18 Array Dimensions... 1 Our Array Shape... (3,) Elements in the Array... 3 Result (expand tabs)... ['Bella Cio' 'Tom Hanks' 'Monry Heist Series']
- Related Questions & Answers
- Return a copy of the string with all occurrences of substring old replaced by new in Numpy
- For each element in a Numpy array, return a copy with the leading spaces removed
- For each element in a Numpy array, return a copy with the trailing spaces removed
- For each element in a Numpy array, return a copy with the leading characters removed in Numpy
- For each element in a Numpy array, return a copy with the trailing characters removed
- For each element in a Numpy array, return a copy with the leading and trailing characters removed
- Replace tab characters by a fixed tabsize in a string array in Numpy
- Return element-wise a copy of the string with uppercase characters converted to lowercase and vice versa in Numpy
- Return a copy of an array with only the first character of each element capitalized in Numpy
- Copy and return all the elements of a masked array in Numpy
- Return input with invalid data masked and replaced by a fill value in Numpy
- Return each element of the masked array rounded in Numpy
- Return a copy of the masked array in NumPy
- Return element-wise string multiple concatenation in Numpy
- Return element-wise string concatenation for two arrays of string in Numpy