What is the function expandtabs in Python

Python String method .expandtabs () for converting tabs into spaces

This method converts all tabs (\ t) contained in a string into spaces. 8 spaces are used for a tab, provided no other information is given as a parameter:

Let's look at it as a sample code:

As a result we get the following output:

Text content 1234567890 more content

Parameters for expandtabs ()

You can use the parameter to specify the number of tabs you want if you would like a number other than 8 (which corresponds to the standard setting).

Examples:

And as a result we get:

Even if the setting for the tab stop for 2, 3 as well as for 4 does not seem to change anything, the behavior fits. In the example, the start of all 3 settings after the first converted tab is 12. And 12 is a multiple of 2, 3 and 4.

Everything up to position 10 is occupied by the previous text plus a space. So the tab can “work” only after 10. Let's look at the rows:

For the 2 series:
2,4,6,8,10 (everything not possible), 12 (and this is where our text starts)

For the 3 series:
3,6,9 (everything not possible), 12 (and this is where our text starts)

For the 4-row:
4,8 (everything not possible), 12 (and our text starts there)

For the 6 series:
6 (not possible), 12 (and this is where our text starts)

The 5 series is different because our text here starts at 15, 5.10 (everything is not possible), 15 (starts our text)

Recommend • Social Bookmarks • Thank you