Python Tutorial Python Advanced Python References Python Libraries

Python String - expandtabs() Method



The Python expandtabs() method is used to set the tab size within the specified string. This method has one optional parameter which is used to specify the tab size. Default value of tab size is 8 whitespaces.

Syntax

string.expandtabs(tabsize)

Parameters

tabsize Optional. specify tab size. default value is 8.

Return Value

Returns the string with specified tab size applied on the given string.

Example:

In the example below, expandtabs() is used to set the tab size of the specified string.

MyString = "x\ty\tz"

print("MyString:", MyString,"\n")
print(MyString.expandtabs(),"\n")
print(MyString.expandtabs(1),"\n")
print(MyString.expandtabs(4),"\n")
print(MyString.expandtabs(10),"\n")

The output of the above code will be:

MyString: x	y	z 

x       y       z 

x y z 

x   y   z 

x         y         z 

❮ Python String Methods