今天在學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 其實代表當前八個字符的單元結束,進入到下一個八個字符的單元中。
