Facebook Page Twitter Page LinkedIn Page
× Python Tutorial Python Advanced Python Resources Python Libraries


The Python String 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 below example, expandtabs() is used to set the tab size of the specified string.

MyString = "x\ty\tz"

print(MyString)
print(MyString.expandtabs())
print(MyString.expandtabs(1))
print(MyString.expandtabs(4))
print(MyString.expandtabs(10))

The output of the above code will be:

x	y	z

x       y       z

x y z

x   y   z

x         y         z

❮ Python String Methods