Python String expandtabs() Method
The expandtabs() method converts the tab symbol \t
in the string to a space, the default number of spaces for the tab symbol \t
is 8, and the tab position is given at 0, 8, 16… If the number of characters from the current position to the start position or the previous tab position is less than a multiple of 8, the tab position is replaced by a space.
Python String expandtabs() Syntax
The expandtabs() method syntax.
str.expandtabs(tabsize=8)
Python String expandtabs() Parameters
- tabsize – Specifies the number of characters to convert the tab symbol
\t
to spaces in the string.
Python String expandtabs() Return Value
This method returns the new string resulting from converting the tab symbol \t
to spaces in the string.
Python String expandtabs() Example#1
expandtabs() takes no arguments.
string = "\t\tApidemos\t\t"
print(string.expandtabs())
Output:
Python String expandtabs() Example#2
Python String expandtabs() method for different tabsize parameter values.
Use expandtabs to insert spacing.
string = "i\tApidemos\tgfg"
print("Modified string using default spacing: ", end ="")
print(string.expandtabs())
print()
print("Modified string using less spacing: ", end ="")
print(string.expandtabs(2))
print()
print("Modified string using more spacing: ", end ="")
print(string.expandtabs(12))
print()
Output:
Python String expandtabs() Example#3
Passing a float or any other non-integer parameter to the tabsize
argument of the expandtabs() method will raise a TypeError.
string = "\tApidemos\t"
print(string.expandtabs(1.1))
Output: