字符串%s

# _*_ encoding: utf-8 _*_   @author: ty  hery   2019/2/18

string = "hello"
# %s打印时结果是hello
print("string01=%s" % string)
# output: string=hello
# %2s意思是字符串长度为2,当原字符串的长度超过2时,按原长度打印,所以%2s的打印结果还是hello
a = 'd的说法奥奥奥奥奥'
print("string02=%20s" % a)
# output: string=hello

# %7s意思是字符串长度为7,当原字符串的长度小于7时,在原字符串左侧补空格,
# 所以%7s的打印结果是  hello
print("string03=%7s" % string)
# output: string=  hello

# %-7s意思是字符串长度为7,当原字符串的长度小于7时,在原字符串右侧补空格,
# 所以%-7s的打印结果是  hello
print("string04=%-7s" % string)
# output: string=hello  !

# %.2s意思是截取字符串的前2个字符,所以%.2s的打印结果是he
print("string05=%.2s" % string)
# output: string=he

# %.7s意思是截取字符串的前7个字符,当原字符串长度小于7时,即是字符串本身,
# 所以%.7s的打印结果是hello
print("string06=%.7s" % string)
# output: string=hello

# %a.bs这种格式是上面两种格式的综合,首先根据小数点后面的数b截取字符串,
# 当截取的字符串长度小于a时,还需要在其左侧补空格
print("string07=%7.2s" % string)
# output: string=     he
print("string08=%2.7s" % string)
# output: string=hello
print("string09=%10.7s" % string)
# output: string=     hello

# 还可以用%*.*s来表示精度,两个*的值分别在后面小括号的前两位数值指定
print("string10=%*.*s" % (1, 8, string))
# output: string=     he

输出:
string01=hello
string02=           d的说法奥奥奥奥奥
string03=  hello
string04=hello  
string05=he
string06=hello
string07=     he
string08=hello
string09=     hello
string10=hello
写入自己的博客中才能记得长久
原文地址:https://www.cnblogs.com/heris/p/14062440.html