Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is expandtabs() in Python?

Wale Moshood

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

If you want to specify the space in the string but you’re uncertain about the amount of space required, Python has an efficient way to handle white spaces between the strings. You can use the expandtabs() function for this purpose.

The expandtabs() function

expandtabs() is a built-in method that substitutes and expands the \t (tab character) between the string, with respect to the amount of white space provided as an argument.

Syntax


string.expandtabs(size)

Parameters

The syntax above represents the expandtabs() function. The function takes an optional parameter size that specifies the amount of space that should be between the strings.

The parameter is of type Integer and has a default size of 88.

Code

Example 1

In this example, we use the expandtabs() function without an argument.

sample_str = 'No\targ\twas\tpassed'
result = sample_str.expandtabs()
print(result)

Explanation

The sample code above demonstrates the use of the expandtabs() function without an argument.

  • The first tab character \t is at position 22 and has a default tab size of 88 because no argument was passed to it.

  • The function then replaces the tab character \t with whitespace until the next tab stop.

  • The number of whitespaces after "No" will be 66 because of the difference between the position of the first tab character (2)(2) and the first tab stop (8)(8).

  • The next tab stops are the multiples of the default tab size (8)(8). Hence, the next tab stops are 16,24,32,4016, 24, 32, 40, and so on.

  • The position of the second and third tab characters are 1111 and 1919, respectively. Consequently, the next tab stops are 1616 and 2424.

  • Hence, there are 55 spaces after "arg" and 55 spaces after "was".

Output


No      arg     was     passed

Example 2

The example below uses the expandtabs() function with an argument.

sample_str = 'An\targ\twas\tpassed'
result = sample_str.expandtabs(3)
print(result)

Explanation

The sample code above demonstrates the use of the expandtabs() function with an argument.

  • The first tab character \t is at position 22 and has a tab size of 33. Thus, the tab stops will be a multiple of 33, e.g., 3,6,9,123, 6, 9, 12, and so on.

  • There will be 11 whitespace after "An" because of the positioned tab character at 22 and a tab stop at position 33.

  • While the positions of the second and third tab characters are 66 and 1212, respectively, the next tab stops will be at positions 99 and 1515.

  • Hence, there will be 33 spaces after "arg" and 33 spaces after "was".

Output


An arg   was   passed

Conclusion

The expandtabs() method is very important in text-formatting where the user requirement keeps changing.

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring