The expandtabs Method in Python: Converting Tabs
Returns a copy of the string where tab characters are replaced with spaces.
expandtabs(tabsize=8)
tabsize=8-- maximum number of spaces a tab can be replaced with.
In the returned string, all tabs are replaced with one or more spaces, depending on the current column number and specified maximum tab size.
my_str = '\t1\t10\t100\t1000\t10000'
my_str.expandtabs()
# ' 1 10 100 1000 10000'
my_str.expandtabs(4)
# ' 1 10 100 1000 10000'
To replace tabs, the initial column number is set to zero, and the string is processed character by character.
If the current character is a tab \t, it is replaced with as many spaces as needed for the current column number to reach the next tab stop. The tab character itself is not copied.
If the current character is a newline \n or a carriage return \r, it is copied, and the current column number is reset to zero.
Other characters are copied unchanged, and the current column number is incremented by one.