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.
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
4 s1 = msg.expandtabs()
5 s2 = msg.expandtabs(5)
6
7
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.
1 The resulting string : Py th on
2 The resulting string : Py th on
Example 2
1 msg = "hello world \t string"
2
3
4 s1 = msg.expandtabs()
5 s2 = msg.expandtabs(5)
6
7
8 print('The resulting string :', s1)
9 print('The resulting string :', s2)
1 The resulting string : hello world string
2 The resulting string : hello world string
Related options for your search