Home >>Python String Methods >Python String expandtabs() Method

Python String expandtabs() Method

Python String expandtabs() Method

Python string expandtabs() method is used to set the tab size within the given input string to the required number of whitespaces.

Syntax:
string.expandtabs(tabsize)

Parameter Values

Parameter Description
tabsize This is an optional parameter. It defines a number specifying the tabsize. The default tabsize value is 8.
Here is an example of Python expandtabs() method:

txt = "P\tH\tP\tT\tP\tO\tI\tN\tT"
x =  txt.expandtabs(2)
print(x)

Output:
P  H  P  T  P  O  I  N  T
Example 2:

txt = "H\te\tl\tl\to"
print(txt.expandtabs())
print(txt.expandtabs(2))
print(txt.expandtabs(4))
print(txt.expandtabs(10))

Output:
H       e       l       l       o
H e l l o
H   e   l   l   o
H         e         l         l         o

No Sidebar ads