Python 扩展制表符长度计算

6 投票
2 回答
9647 浏览
提问于 2025-04-15 21:42

我对使用expandtabs时字符串长度是怎么计算的感到困惑。我原以为expandtabs会把制表符(tab)替换成相应数量的空格(默认每个制表符替换成8个空格)。但是,当我用不同长度的字符串和不同数量的制表符运行命令时,计算出的长度和我想的不一样(也就是说,每个制表符并不总是让字符串长度增加8)。

下面是一个详细的脚本输出,里面有注释解释我认为应该是上面命令执行结果的内容。有人能解释一下使用expandtabs时长度是怎么计算的吗?

IDLE 2.6.5     
>>> s = '\t'
>>> print len(s)
1
>>> #the length of the string without expandtabs was one (1 tab counted as a single space), as expected.
>>> print len(s.expandtabs())
8
>>> #the length of the string with expandtabs was eight (1 tab counted as eight spaces).
>>> s = '\t\t'
>>> print len(s)
2
>>> #the length of the string without expandtabs was 2 (2 tabs, each counted as a single space).
>>> print len(s.expandtabs())
16
>>> #the length of the string with expandtabs was 16 (2 tabs counted as 8 spaces each).
>>> s = 'abc\tabc'
>>> print len(s)
7
>>> #the length of the string without expandtabs was seven (6 characters and 1 tab counted as a single space).
>>> print len(s.expandtabs())
11
>>> #the length of the string with expandtabs was NOT 14 (6 characters and one 8 space tabs).
>>> s = 'abc\tabc\tabc'
>>> print len(s)
11
>>> #the length of the string without expandtabs was 11 (9 characters and 2 tabs counted as a single space).
>>> print len(s.expandtabs())
19
>>> #the length of the string with expandtabs was NOT 25 (9 characters and two 8 space tabs).
>>>

2 个回答

6

制表符会把列指针移动到下一个8的倍数的位置:

>>> 'abc\tabc'.expandtabs().replace(' ', '*')
'abc*****abc'
10

就像你在文本编辑器里按下制表符(Tab)键时,制表符会把光标移动到下一个8的倍数的位置。

所以:

  • '\t' 单独使用时,长度是8,显而易见。
  • '\t\t' 的长度是16。
  • 'abc\tabc' 开始时有3个字符,然后按下制表符后,光标跳到8的位置,最后的 'abc' 把长度从8增加到11...
  • 'abc\tabc\tabc' 同样开始时是3,按下制表符后跳到8,再加一个 'abc' 到11,然后再按一个制表符跳到16,最后的 'abc' 把长度增加到19。

撰写回答