Python String expandtabs()

The syntax of expandtabs() method is:

string.expandtabs(tabsize)

expandtabs() Parameters

The expandtabs() takes an integer tabsize argument. The default tabsize is 8.


Return Value from expandtabs()

The expandtabs() returns a string where all '\t' characters are replaced with whitespace characters until the next multiple of tabsize parameter.


Example 1: expandtabs() With no Argument

str = 'xyz\t12345\tabc'

# no argument is passed
# default tabsize is 8
result = str.expandtabs()

print(result)

Output

xyz     12345   abc

How expandtabs() works in Python?

The expandtabs() method keeps track of the current cursor position.

The position of first '\t' character in the above program is 3. And, the tabsize is 8 (if argument is not passed).

The expandtabs() character replaces the '\t' with whitespace until the next tab stop. The position of '\t' is 3 and the first tab stop is 8. Hence, the number of spaces after 'xyz' is 5.

The next tab stops are the multiples of tabsize. The next tab stops are 16, 24, 32 and so on.

Now, the position of second '\t' character is 13. And, the next tab stop is 16. Hence, there are 3 spaces after '12345'.


Example 2: expandtabs() With Different Argument

str = "xyz\t12345\tabc"
print('Original String:', str)

# tabsize is set to 2
print('Tabsize 2:', str.expandtabs(2))

# tabsize is set to 3
print('Tabsize 3:', str.expandtabs(3))

# tabsize is set to 4
print('Tabsize 4:', str.expandtabs(4))

# tabsize is set to 5
print('Tabsize 5:', str.expandtabs(5))

# tabsize is set to 6
print('Tabsize 6:', str.expandtabs(6))

Output

Original String: xyz	12345	abc
Tabsize 2: xyz 12345 abc
Tabsize 3: xyz   12345 abc
Tabsize 4: xyz 12345   abc
Tabsize 5: xyz  12345     abc
Tabsize 6: xyz   12345 abc

Explanation

  • The default tabsize is 8. The tab stops are 8, 16 and so on. Hence, there is 5 spaces after 'xyz' and 3 after '12345' when you print the original string.
  • When you set the tabsize to 2. The tab stops are 2, 4, 6, 8 and so on. For 'xyz', the tab stop is 4, and for '12345', the tab stop is 10. Hence, there is 1 space after 'xyz' and 1 space after '12345'.
  • When you set the tabsize to 3. The tab stops are 3, 6, 9 and so on. For 'xyz', the tab stop is 6, and for '12345', the tab stop is 12. Hence, there are 3 spaces after 'xyz' and 1 space after '12345'.
  • When you set the tabsize to 4. The tab stops are 4, 8, 12 and so on. For 'xyz', the tab stop is 4 and for '12345', the tab stop is 12. Hence, there is 1 space after 'xyz' and 3 spaces after '12345'.
  • When you set the tabsize to 5. The tab stops are 5, 10, 15 and so on. For 'xyz', the tab stop is 5 and for '12345', the tab stop is 15. Hence, there are 2 spaces after 'xyz' and 5 spaces after '12345'.
  • When you set the tabsize to 6. The tab stops are 6, 12, 18 and so on. For 'xyz', the tab stop is 6 and for '12345', the tab stop is 12. Hence, there are 3 spaces after 'xyz' and 1 space after '12345'.

Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges