我很困惑使用expandtabs时如何计算字符串的长度.我认为expandtabs用适当数量的空格替换制表符(每个制表符的默认空格数为8).但是,当我使用不同长度和不同数量的选项卡的字符串运行命令时,长度计算与我认为的不同(即,每个选项卡并不总是导致每个实例的字符串长度增加8 "/ t").
下面是一个详细的脚本输出,其中的注释解释了我认为应该是上面执行的命令的结果.有人请解释在使用扩展标签时如何计算长度?
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).
>>> …Run Code Online (Sandbox Code Playgroud)