What is the numpy.char.expandtabs() function in Numpy?

Overview

The char.expandtabs() function in Python is used to return a copy of each string element of an array in such a way that all the tab characters are replaced by one or more spaces.

Syntax

char.expandtabs(a, tabsize=8)

Parameters

  • a: This represents the input array.
  • tabsize: This is used to replace the tabs. The default value is 8 spaces. This is optional.

Return value

This function returns an output array of strings or Unicode, depending on the input.

Example

# A code to illustrate the char.expnadtabs() function
# importing the numpy library
import numpy as np
# implementing the char.expandtabs() function
new_array = np.char.expandtabs(["Theo\thow\tare\tyou\ttoday?"], 16)
# printing the expanded tabs
print(new_array)

Explanation

  • Line 4: We import the numpy module.
  • Line 7: We expanded the tabs of a string element using a tab size of 16 by using the char.expandtabs() function. We'll assign the result to a variable, my_array.
  • Line 10: We print the variable, my_array.

Free Resources