String method expandtabs() in Python
The string method expandtabs() used to set the tab size to the specified number of whitespaces. It replace the tab characters from the string to the number of spaces in the string. It returns a string that includes the space instead of tab character.
Syntax
 1 <string>.expandtabs(<tabsize>)
string : A source string with tab character to set the tab size
tabsize : A number that specify the tab size, if not specified, the default value will be 8
String method expandtabs()
 1 msg = "Py\tth\ton"
 2 
 3 # setting tab size
 4 s1 = msg.expandtabs()
 5 s2 = msg.expandtabs(5)
 6 
 7 # printing string
 8 print('The resulting string :', s1)
 9 print('The resulting string :', s2)
In the above example, a string variable is define with initial value that include tab character. A expandtabs() method called by using source string. The first method call sets the tabsize to 8 while in the second call, we have sets tabsize to 5. The result will be stored in variable that will be printed on console.
Output
 1 The resulting string : Py      th      on
 2 The resulting string : Py   th   on

Example 2

Example 2
 1 msg = "hello world \t string"
 2 
 3 # setting tab size
 4 s1 = msg.expandtabs()
 5 s2 = msg.expandtabs(5)
 6 
 7 # printing string
 8 print('The resulting string :', s1)
 9 print('The resulting string :', s2)
Output
 1 The resulting string : hello world      string
 2 The resulting string : hello world     string
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us