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

In Python, handling white spaces between strings is easy. Sometimes, we may want to add space in a string, but we are not sure exactly how much. Python provides different ways to manage this, and one useful method is the expandtabs() method.

Using the expandtabs() Method

The expandtabs() method in Python is used to replace tab characters (\t) in a string with spaces. It returns a new string where each \t is replaced with the number of spaces needed to reach the next tab stop. You can control how many spaces are used by passing a tabsize value to the method.

Syntax

string.expandtabs(size_of_tab)

Where size_of_tab is the number of spaces used to replace each \t character. By default, it is set to 8.

Example: Without any Parameter

In the following example, we do not pass any parameter to the expandtabs() method, so the default tab size of 8 is applied ?

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

The output of the above code is ?

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

Example: With Different Parameters

In the following example, we pass different tab sizes (6, 3, and 2) to the expandtabs() method ?

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

The output of the above code is ?

Tab size 6: Expanding   tabs  in    string      to    multiple    spaces.
Tab size 3: Expanding   tabs  in string   to multiple spaces.
Tab size 2: Expanding tabs  in  string  to  multiple  spaces.

TypeError with Non-Integer Values

The expandtabs() method raises a TypeError if you pass a non-integer value such as a float ?

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

The output of the above code is ?

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    result = string.expandtabs(6.2)
TypeError: 'float' object cannot be interpreted as an integer

Using the replace() Method

The replace() method can also replace tab characters with a fixed number of spaces. Unlike expandtabs(), it replaces each \t with exactly the string you specify, without calculating tab stops ?

string = "Expanding\ttabs\tin\tstring\tto\tmultiple\tspaces."
result = string.replace('\t', ' ')
print(result)

The output of the above code is ?

Expanding tabs in string to multiple spaces.

Using the re Module

You can also use regular expressions with Python's built-in re module. The re.sub() function replaces all occurrences of a pattern in a string ?

import re
string = "Expanding\ttabs\tin\tstring\tto\tmultiple\tspaces."
result = re.sub('\t', ' ', string)
print(result)

The output of the above code is ?

Expanding tabs in string to multiple spaces.

Conclusion

Use expandtabs() when you need tab-stop-aligned spacing, replace() for simple fixed substitution, and re.sub() when working with more complex patterns. All three methods return a new string and do not modify the original.

Updated on: 2026-03-13T21:09:16+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements