How to expand tabs in string to multiple spaces in Python?


The handling of white spaces between strings is managed effectively in Python. In this article, we'll examine the methods to provide a space in the string when we are unsure of how much space is necessary.

Using string expandtabs() method

A built-in method in Python called String expandtabs() is used to handle spaces in strings. The expandtabs() method in Python creates a copy of the string that has all tab characters '\t' up to the next multiple tabsize parameters replaced with whitespace characters.

There may be instances when it is necessary to define how much space should be left in the string, but the specific amount depends on the circumstances.

It is a laborious process to constantly adjust the string in these situations. As a result, the "expandtabs()" method in the Python library defines how much space should be added to the string before the "\t" symbol is substituted.

Syntax

Following is the syntax of expandtabs() −

string.expandtabs(size_of_tab)

where,

size_of_tab_ is the whitespace character size to replace \t. The default is 8.

Return value

Until the next multiple of the tab size argument, all '\t' characters are replaced with whitespace characters in the string that is returned.

Example

without any parameter

In the following example, we don't pass any parameters to the expandtabs() function −

string = "Expanding\ttabs\tin\tstring\tto\tmultiple\tspaces." result = string.expandtabs() print('The expanded string is:',result)

Output

The Python string expandtabs() function is used in the example above without a parameter, i.e. the size. As a result, the default size, 8, is used in place of the '\t'.

The expanded string is: Expanding       tabs    in      string  to      multiple        spaces.

Example

with different parameters

In the following example, we pass different parameters to the expandtabs() function −

string = "Expanding\ttabs\tin\tstring\tto\tmultiple\tspaces." result = string.expandtabs(6) result1 = string.expandtabs(3) result2 = string.expandtabs(2) print('The expanded string is:',result) print('The expanded string is:',result1) print('The expanded string is:',result2)

Output

Size = 6, Size = 3, and Size = 2 are passed as parameters to the Python string expandtabs() function in the sample of code above. As a result, spaces of sizes 6, 3, and 2 units respectively are used in place of the character "\t" across the entire string.

The expanded string is: Expanding   tabs  in    string      to    multiple    spaces.
The expanded string is: Expanding   tabs  in string   to multiple spaces.
The expanded string is: Expanding tabs  in  string  to  multiple  spaces.

Errors and exceptions in expandtabs()

The Python string expandtabs() function throws a TypeError exception if we attempt to pass a value that is not of the integer type, such as a floating-point number, as a parameter.

Thus, it is clear that the expandtabs() function only accepts parameters of the integer type. Following is an example of expantabs() method by passing a non-integer value as a parameter −

Example

string = "Expanding\ttabs\tin\tstring\tto\tmultiple\tspaces." result = string.expandtabs(6.2) print('The expanded string is:',result)

Output

The following error is thrown while executing the above code −

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    result = string.expandtabs(6.2)
TypeError: integer argument expected, got float

Using numpy.expandtabs() function

The numpy.char.expandtabs() function in the Python Numpy module performs the same functions as the built-in expandtabs() function.

If the user wants to be more exact, they may also pass an array as an argument to the numpy.char.expandtabs() function along with the size of the spaces.

Syntax

Following is the syntax of numpy.expandtabs() −

numpy.char.expandtabs(array,size_of_tab)

where,

  • array includes the elements that the function operates on.

  • size_of_tab(optional) is the size of the spaces to be provided is specified by replacing the tab-space character, or "\t," with this optional parameter.

Example

Following is an example of numpy.expantabs() function −

import numpy given_array = numpy.array("Expanding\ttabs\tin\tstring\tto\tmultiple\tspaces.") result = numpy.char.expandtabs(given_array,16) print('The expanded string array is:',result)

Output

Following is an output of the above code −

The expanded string array is: Expanding       tabs            in              string          to              multiple        spaces.

Using replace() method

The string class in Python contains a method called replace. It requires two strings as input: one to be replaced and one to be used in its place. It's used with a string object.

Example

Following is an example to expand tabs in string to multiple spaces using replace() method −

string = "Expanding tabs in string to multiple spaces." result = string.replace('\t', '') print( result)

Output

Following is an output of the above code

Expanding tabs in string to multiple spaces.

Using re module

Regexes can also be used with Python's "re" module to achieve the same outcome. To replace the characters in string, use the function re.sub(regex to replace, regex to replace with, string).

Example

Following is an example to expand tabs in string to multiple spaces using re module −

import re string = "Expanding tabs in string to multiple spaces." result = re.sub('\t', '',string) print(result)

Output

Following is an output of the above code −

Expanding tabs in string to multiple spaces.

Updated on: 23-Nov-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements