Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python expandtabs string operation

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?

like image 762
WutWut Avatar asked Dec 31 '15 11:12

WutWut


People also ask

What is Tabsize in Python?

tabsize − This specifies the number of characters to be replaced for a tab character '\t'. This method returns a copy of the string in which tab characters i.e., '\t' have been expanded using spaces.

What is Expandtab?

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.

What is center function in Python?

Python String center() Method The center() method will center align the string, using a specified character (space is default) as the fill character.


1 Answers

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.

like image 55
jbg Avatar answered Oct 17 '22 08:10

jbg