我刚刚观察到python中的expandtabs()
字符串方法以我无法理解的怪异方式工作,我知道它会增加空格,但是给定长度,它以不同的方式工作。任何人都可以解释发生了什么吗?
str = "this is\tstring example....wow!!!"
print ("Original string : " + str)
print ("Defualt exapanded tab : " + str.expandtabs())
for i in range(-2,30):
print ("Double exapanded tab : " + str.expandtabs(i) , i)
输出:
Original string : this is string example....wow!!!
Defualt exapanded tab : this is string example....wow!!!
Double exapanded tab : this isstring example....wow!!! Length : -2
Double exapanded tab : this isstring example....wow!!! Length : -1
Double exapanded tab : this isstring example....wow!!! Length : 0
Double exapanded tab : this is string example....wow!!! Length : 1
Double exapanded tab : this is string example....wow!!! Length : 2
Double exapanded tab : this is string example....wow!!! Length : 3
Double exapanded tab : this is string example....wow!!! Length : 4
Double exapanded tab : this is string example....wow!!! Length : 5
Double exapanded tab : this is string example....wow!!! Length : 6
Double exapanded tab : this is string example....wow!!! Length : 7
Double exapanded tab : this is string example....wow!!! Length : 8
Double exapanded tab : this is string example....wow!!! Length : 9
Double exapanded tab : this is string example....wow!!! Length : 10
Double exapanded tab : this is string example....wow!!! Length : 11
Double exapanded tab : this is string example....wow!!! Length : 12
Double exapanded tab : this is string example....wow!!! Length : 13
Double exapanded tab : this is string example....wow!!! Length : 14
Double exapanded tab : this is string example....wow!!! Length : 15
Double exapanded tab : this is string example....wow!!! Length : 16
Double exapanded tab : this is string example....wow!!! Length : 17
Double exapanded tab : this is string example....wow!!! Length : 18
Double exapanded tab : this is string example....wow!!! Length : 19
Double exapanded tab : this is string example....wow!!! Length : 20
Double exapanded tab : this is string example....wow!!! Length : 21
Double exapanded tab : this is string example....wow!!! Length : 22
Double exapanded tab : this is string example....wow!!! Length : 23
Double exapanded tab : this is string example....wow!!! Length : 24
Double exapanded tab : this is string example....wow!!! Length : 25
Double exapanded tab : this is string example....wow!!! Length : 26
Double exapanded tab : this is string example....wow!!! Length : 27
Double exapanded tab : this is string example....wow!!! Length : 28
Double exapanded tab : this is string example....wow!!! Length : 29
答案 0 :(得分:0)
string.expandtabs(tabsize)方法返回字符串的副本,其中所有制表符'\ t'都用空格字符替换,直到tabsize参数的下一个倍数为止。
string.expandtabs(i)方法返回一个i大小的字符串副本,该副本被白字符替换。