Python numpy.char.expandtabs() Method (With Examples)

By IncludeHelp Last updated : December 1, 2023

Python numpy.char.expandtabs() Method

The expandtabs() method is a built-in method of the char class in the numpy module, it is used to expand the tab size in a given array of string or Unicode, it returns an array where all tab characters are replaced by one or more spaces, element-wise.

Module

The following module is required to use the expandtabs() method:

import numpy as np

Syntax

The following is the syntax of the expandtabs() method:

char.expandtabs(arr, tab_size = 8)

Parameter(s)

The following are the parameter(s):

  • arr: An array of strings or Unicode.
  • tab_size: To specify the new tab size, it's an optional parameter with a default value of 8.

Return Value

The return type of the expandtabs() method is <class 'numpy.ndarray'>, it returns an array where all tab characters are replaced by one or more spaces, element-wise.

Example 1: Applying char.expandtabs() method on a single-element array

In this example, we are replacing the tab size with 15 spaces.

# Importing numpy module
import numpy as np

# Creating an array of single-element string
arr = ["Hello\tWorld"]

# Printing array
print("arr (original):", arr)

# Expanding tab size
# Using char.expandtabs() method
result = np.char.expandtabs(arr, 15)

# Printing the result
print("\nThe result is:", result)
print("\nThe type of the result is:", type(result))

Output

The output of the above example is:

arr (original): ['Hello\tWorld']

The result is: ['Hello          World']

The type of the result is: <class 'numpy.ndarray'>

Example 2: Applying char.expandtabs() method on an array of strings

In this example, we are replacing the tab size with 1 space.

# Importing numpy module
import numpy as np

# Creating an array of strings
arr = ["Hello\tWorld", "I'm\tAlvin", "I'm\tLearning\tPython"]

# Printing array
print("arr (original):", arr)

# Replacing tabs with one space
# Using char.expandtabs() method
result = np.char.expandtabs(arr, 1)

# Printing the result
print("\nThe result is:", result)
print("\nThe type of the result is:", type(result))

Output

The output of the above example is:

arr (original): ['Hello\tWorld', "I'm\tAlvin", "I'm\tLearning\tPython"]

The result is: ['Hello World' "I'm Alvin" "I'm Learning Python"]

The type of the result is: <class 'numpy.ndarray'>

Example 3: Applying char.expandtabs() method with default tab size

In this example, we are replacing the tab size with the default tab size (that is 8).

# Importing numpy module
import numpy as np

# Creating an array of strings
arr = ["Hello\tWorld", "I'm\tAlvin", "I'm\tLearning\tPython"]

# Printing array
print("arr (original):", arr)

# Default tab size
# Using char.expandtabs() method
result = np.char.expandtabs(arr)

# Printing the result
print("\nThe result is:", result)
print("\nThe type of the result is:", type(result))

Output

The output of the above example is:

arr (original): ['Hello\tWorld', "I'm\tAlvin", "I'm\tLearning\tPython"]

The result is: ['Hello   World' "I'm     Alvin" "I'm     Learning        Python"]

The type of the result is: <class 'numpy.ndarray'>

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.