今天在学learn python the hard way ex26修改的时候,有一个关于\t的问题,下面分别为代码以及输出结果:
1 poem = """ 2 \tThe lovely world 3 with logic so firmly planted 4 cannot discern \n the needs of love 5 nor comprehend \tpassion from intuition 6 and requires an explantion 7 \n\t\twhere there is none. 8 """ 9 10 11 print "--------------" 12 print poem 13 print "--------------"
--------------
The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explantion
where there is none.
--------------
可以看到,第二行的 \t 以及第七行的两个\t 都分别'缩进'了八个字符,而第七行的 \t 只'缩进'了一个字符,那么 \t 到底是多少个字符呢(其实这个说法不对)?
为了搞明白这个问题,测试的代码如下:
1 print """ 2 12345678901234567890123456789012345678901 3 \t9\t789\t5\t\t1 4 1234567\t9 5 12345678\t7 6 """
第二行的数字是为了方便计算字符数的参考,运行结果如下:
12345678901234567890123456789012345678901
9 789 5 1
1234567 9
12345678 7
可以看到line3 的前三个 \t 分别为八个、七个、五个字符,而 line4 line5 的为一个和八个字符
其实到这里(虽然我还测试了一下其他的代码,但是我直接改成上面的代码了没保存,懒的再打了)可以大致猜测一下, \t 应该是八个字符为一个循环,每个 \t 即代表当前八个字符的单元已经ok,然后进入下一串八个字符的单元
所以line3 后面两个\t 应该分别缩进了七个和八个字符,这也可以解释之前代码中 \t 为什么只'缩进'了一个字符。
总结:
\t 其实代表当前八个字符的单元结束,进入到下一个八个字符的单元中。