7

I am learning about Python and got to the expandtabs command in Python. This is the official definition in the docs:

string.expandtabs(s[, tabsize])

Expand tabs in a string replacing them by one or more spaces, depending on the current column and the given tab size. The column number is reset to zero after each newline occurring in the string. This doesn’t understand other non-printing characters or escape sequences. The tab size defaults to 8.

So what I understood from that is that the default size of tabs is 8 and to increase that, we can use other values

So, when I tried that in the shell, I tried the following inputs -

>>> str = "this is\tstring"
>>> print str.expandtabs(0)
this isstring
>>> print str.expandtabs(1)
this is string
>>> print str.expandtabs(2)
this is string
>>> print str.expandtabs(3)
this is  string
>>> print str.expandtabs(4)
this is string
>>> print str.expandtabs(5)
this is   string
>>> print str.expandtabs(6)
this is     string
>>> print str.expandtabs(7)
this is       string
>>> print str.expandtabs(8)
this is string
>>> print str.expandtabs(9)
this is  string
>>> print str.expandtabs(10)
this is   string
>>> print str.expandtabs(11)
this is    string

So here,

  • 0 removes the tab character entirely,
  • 1 is exactly like the default 8,
  • but 2is exactly like 1 and then
  • 3 is different
  • and then again 4 is like using 1

and after that it increases up till 8 which is the default and then increases after 8.But why the weird pattern in numbers from 0 to 8? I know it is supposed to start from 8, but what is the reason?

2 Answers 2

9

str.expandtabs(n) is not equivalent to str.replace("\t", " " * n).

str.expandtabs(n) keeps track of the current cursor position on each line, and replaces each tab character it finds with the number of spaces from the current cursor position to the next tab stop. The tab stops are taken to be every n characters.

This is fundamental to the way tabs work, and is not specific to Python. See this answer to a related question for a good explanation of tab stops.

string.expandtabs(n) is equivalent to:

def expandtabs(string, n):
    result = ""
    pos = 0
    for char in string:
        if char == "\t":
            # instead of the tab character, append the
            # number of spaces to the next tab stop
            char = " " * (n - pos % n)
            pos = 0
        elif char == "\n":
            pos = 0
        else:
            pos += 1
        result += char
    return result

And an example of use:

>>> input = "123\t12345\t1234\t1\n12\t1234\t123\t1"
>>> print(expandtabs(input, 10))
123       12345     1234      1
12        1234      123       1

Note how each tab character ("\t") has been replaced with the number of spaces that causes it to line up with the next tab stop. In this case, there is a tab stop every 10 characters because I supplied n=10.

Sign up to request clarification or add additional context in comments.

4 Comments

See also this question related to your explanation.
Could you please explain that in a simpler way? I can't understand how that would change the output in the cases I have added in the question.
@WutWut: Perhaps take a look at what a tab stop is, and that ought to help you understand how a tab (tabulator) works.
@WutWut I’ve added an implementation in Python of the expandtabs function, to show the logic.
2

The expandtabs method replaces the \t with whitespace characters until the next multiple of tabsize parameter i.e., the next tab position.

for eg. take str.expandtabs(5)

'this (5)is(7)\tstring' so the '\t' is replaced with whitespace until index=10 and follwing string is moved forward. so you see 10-7=3 whitespaces. (**number in brackets are index numbers **)

eg2. str.expandtabs(4)

'this(4) is(7)\tstring' here '\t' replaces until index=8. so you see only one whitespace

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.