您误解了标签的含义。制表符表示:从当前位置移动到下一个制表位。
这些制表位的位置取决于您的文本编辑器或终端。通常它们位于每 8 列,Stack Overflow 将它们标记到 第四 列:
>>> def show_tabs():
... print('\t'.join(['v'] * 8))
... for i in range(1, 33):
... print(''.join(map(lambda j: str(j)[-1], range(1, i))), 'tab to here', sep='\t')
...
>>> show_tabs()
v v v v v v v v
tab to here
1 tab to here
12 tab to here
123 tab to here
1234 tab to here
12345 tab to here
123456 tab to here
1234567 tab to here
12345678 tab to here
123456789 tab to here
1234567890 tab to here
12345678901 tab to here
123456789012 tab to here
1234567890123 tab to here
12345678901234 tab to here
123456789012345 tab to here
1234567890123456 tab to here
12345678901234567 tab to here
123456789012345678 tab to here
1234567890123456789 tab to here
12345678901234567890 tab to here
123456789012345678901 tab to here
1234567890123456789012 tab to here
12345678901234567890123 tab to here
123456789012345678901234 tab to here
1234567890123456789012345 tab to here
12345678901234567890123456 tab to here
123456789012345678901234567 tab to here
1234567890123456789012345678 tab to here
12345678901234567890123456789 tab to here
123456789012345678901234567890 tab to here
1234567890123456789012345678901 tab to here
在您自己的终端中运行上述代码将很快显示您的选项卡设置。
在您的文本中,有一行短于 8 个字符,因此下一个制表位位于从开头开始的 8 个字符处。一行超过 16 个字符,因此下一个制表位为 24 个字符。大多数行包含 9 到 14 个字符,因此下一个制表位在 16 个字符处。换句话说,您的输出与在列之间使用制表符完全一致。
如果您想生成包含完美对齐的列的文本输出,请调整您的制表符以适应特定的制表位配置(根据第一列的长度使用更多或更少的制表符),或者仅使用空格,并且再次调整间距。 Python 可以帮助处理后者,请参阅 formatting with str.format(),您可以在给定宽度的空白处左对齐或右对齐文本。