Python展开标签长度计算

2022-01-09 00:00:00 python tabs

问题描述

我对使用 expandtabs 时如何计算字符串的长度感到困惑.我认为 expandtabs 用适当数量的空格替换制表符(每个制表符的默认空格数为 8).但是,当我使用不同长度和不同数量的制表符的字符串运行命令时,长度计算与我想象的不同(即,每个制表符并不总是导致每个实例的字符串长度增加 8的/t").

I'm confused by how the length of a string is calculated when expandtabs is used. I thought expandtabs replaces tabs with the appropriate number of spaces (with the default number of spaces per tab being 8). However, when I ran the commands using strings of varying lengths and varying numbers of tabs, the length calculation was different than I thought it would be (i.e., each tab didn't always result in the string length being increased by 8 for each instance of "/t").

下面是一个详细的脚本输出,其中包含解释我认为应该是上面执行的命令的结果的注释.有人能解释一下使用展开标签时如何计算长度吗?

Below is a detailed script output with comments explaining what I thought should be the result of the command executed above. Would someone please explain the how the length is calculated when expand tabs is used?

IDLE 2.6.5     
>>> s = '	'
>>> 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 = '		'
>>> 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	abc'
>>> 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	abc	abc'
>>> 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).
>>>


解决方案

就像在文本编辑器中输入制表符一样,制表符将长度增加到下一个 8 的倍数.

Like when you are entering tabs in a text-editor, the tab character increases the length to the next multiple of 8.

所以:

  • ' ' 本身显然是 8.
  • ' ' 是 16.
  • 'abc abc' 从 3 个字符开始,然后一个制表符将其推送到 8 个字符,然后最后一个 'abc' 将其从 8 个字符推送到 11 个字符...
  • 'abc abc abc' 同样从 3 开始,tab 将它推到 8,另一个 'abc' 到 11,然后另一个 tab 将它推到 16,最后的 'abc' 使长度达到 19.
  • ' ' by itself is 8, obviously.
  • ' ' is 16.
  • 'abc abc' starts at 3 characters, then a tab pushes it up to 8, and then the last 'abc' pushes it from 8 to 11...
  • 'abc abc abc' likewise starts at 3, tab bumps it to 8, another 'abc' goes to 11, then another tab pushes it to 16, and the final 'abc' brings the length to 19.

相关文章