Python 扩展标签长度计算 [英] Python Expand Tabs Length Calculation

查看:23
本文介绍了Python 扩展标签长度计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用 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 = '\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).
>>>

推荐答案

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

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

所以:

  • '\t' 显然是 8.
  • '\t\t' 是 16.
  • 'abc\tabc' 从 3 个字符开始,然后一个制表符将它推到 8,然后最后一个 'abc' 将它从 8 推到 11...
  • 'abc\tabc\tabc' 同样从 3 开始,制表符将其推到 8,另一个 'abc' 转到 11,然后另一个制表符将其推到 16,最后的 'abc' 将长度变为 19.
  • '\t' by itself is 8, obviously.
  • '\t\t' is 16.
  • 'abc\tabc' starts at 3 characters, then a tab pushes it up to 8, and then the last 'abc' pushes it from 8 to 11...
  • 'abc\tabc\tabc' 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.

这篇关于Python 扩展标签长度计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆