python中%s的各种用法汇总

小豆苗 1年前 ⋅ 6645 阅读
string="hello"  
  
#%s打印时结果是hello  
print "string=%s" % string      # output: string=hello  
  
#%2s意思是字符串长度为2,当原字符串的长度超过2时,按原长度打印,所以%2s的打印结果还是hello  
print "string=%2s" % string     # output: string=hello  
  
#%7s意思是字符串长度为7,当原字符串的长度小于7时,在原字符串左侧补空格,  
#所以%7s的打印结果是  hello  
print "string=%7s" % string     # output: string=  hello  
  
#%-7s意思是字符串长度为7,当原字符串的长度小于7时,在原字符串右侧补空格,  
#所以%-7s的打印结果是  hello  
print "string=%-7s!" % string     # output: string=hello  !  
  
#%.2s意思是截取字符串的前2个字符,所以%.2s的打印结果是he  
print "string=%.2s" % string    # output: string=he  
  
#%.7s意思是截取字符串的前7个字符,当原字符串长度小于7时,即是字符串本身,  
#所以%.7s的打印结果是hello  
print "string=%.7s" % string    # output: string=hello  
  
#%a.bs这种格式是上面两种格式的综合,首先根据小数点后面的数b截取字符串,  
#当截取的字符串长度小于a时,还需要在其左侧补空格  
print "string=%7.2s" % string   # output: string=     he  
print "string=%2.7s" % string   # output: string=hello  
print "string=%10.7s" % string  # output: string=     hello  
  
#还可以用%*.*s来表示精度,两个*的值分别在后面小括号的前两位数值指定  
print "string=%*.*s" % (7,2,string)      # output: string=     he 

全部评论: 0

    我有话说: