Related Tags

python
communitycreator

# What is expandtabs() in Python?

Wale Moshood

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 $8$.

### 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 $2$ and has a default tab size of $8$ 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 $6$ because of the difference between the position of the first tab character $(2)$ and the first tab stop $(8)$.

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

• The position of the second and third tab characters are $11$ and $19$, respectively. Consequently, the next tab stops are $16$ and $24$.

• Hence, there are $5$ spaces after "arg" and $5$ 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 $2$ and has a tab size of $3$. Thus, the tab stops will be a multiple of $3$, e.g., $3, 6, 9, 12$, and so on.

• There will be $1$ whitespace after "An" because of the positioned tab character at $2$ and a tab stop at position $3$.

• While the positions of the second and third tab characters are $6$ and $12$, respectively, the next tab stops will be at positions $9$ and $15$.

• Hence, there will be $3$ spaces after "arg" and $3$ 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

CONTRIBUTOR

Wale Moshood