关于 \t 水平制表符 Horizontal Tab (TAB)

今天在学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 其实代表当前八个字符的单元结束,进入到下一个八个字符的单元中。

posted on 2016-05-31 22:19 Wenchaoz 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/wenchaoz/p/5547804.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值