Python expandtabs string operation

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

 string.expandtabs(s[, tabsize]) 

Expand the tabs in a row, replacing them with one or more spaces, depending on the current column and the specified tab size. The number of reset columns is zero after each row of a new row in a row. It does not understand other non-printable characters or escape sequences. The default tab size is 8.

So, I realized from this that the default tab size is 8, and to increase it, we can use other values

So, when I tried this in a 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 completely removes the tab character,
  • 1 exactly matches default 8 ,
  • but 2 exactly matches 1 and then
  • 3 different
  • and then again 4 like using 1

and after that it increments to 8 , which is the default, and then incrementing after 8. But why is there a weird pattern in numbers from 0 to 8? I know it should start at 8, but what is the reason?

+7
source share
2 answers

str.expandtabs(n) 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 that it finds with the number of spaces from the current cursor position to the next tab stop. Tabs are considered n characters.

This is fundamental to tabbed browsing and does not apply to Python. See this answer to the appropriate question for a good explanation of tab stops.

string.expandtabs(n) 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) if char == "\n": pos = 0 else: pos += len(char) 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 that each tab character ( "\t" ) has been replaced by the number of spaces, which is why it aligns with the next tab stop. In this case, there is a stop tab every 10 characters, because I set n=10 .

+7
source share

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

eg. take str.expandtabs(5)

'this (5) is (7) \ tstring', so '\ t' is replaced by a space until index = 10 and the line follwing is moved forward. therefore you see 10-7 = 3 spaces. (** the number in parentheses is the index numbers **)

EG2. str.expandtabs(4)

'this (4) is (7) \ tstring' here '\ t' is replaced before index = 8. so you see only one spaces

+2
source share

All Articles