Python String expandtabs() method
Python String expandtabs() method
The string.expandtabs() method in Python returns a copy of the string where all tab characters are expanded using spaces.
Syntax
String.expandtabs([tabsize])
Parameter
tabsize: This parameter specifies the number of characters to be replaced for a tab character '\t'.
Return
This method returns a copy of the string where all tab characters are expanded using spaces.
Example 1
# Python program explaining # the expandtabs() method # no argument is passed as the # default tabsize is 8 str= "Hello World" result = str.expandtabs() print(result)
Output
Hello World
Example 2
# Python program explaining # the expandtabs() method # initializing string strval = "i\tlove\Tutorialsandexamples" # printing the strval print(strval) # expanding the value print(strval.expandtabs()) print(strval.expandtabs(12)) print(strval.expandtabs(14)) print(strval.expandtabs(120))
Output
i love\Tutorialsandexamples i love\Tutorialsandexamples i love\Tutorialsandexamples i love\Tutorialsandexamples i love\Tutorialsandexamples
Example 3
# Python program explaining # the expandtabs() method # initializing the string strVal = "i\tlove\Tutorialsandexamples" # using expandtabs to insert spacing print("String with default spacing: ", end ="") print(strVal.expandtabs()) # using expandtabs to insert less spacing print("String with less spacing: ", end ="") print(strVal.expandtabs(2)) # using expandtabs to insert more spacing print("String with maximum spacing: ", end ="") print(strVal.expandtabs(12))
Output
String with default spacing: i love\Tutorialsandexamples String with less spacing: i love\Tutorialsandexamples String with more spacing: i love\Tutorialsandexamples