Pythonの%s%d%f
3386 ワード
'''%s '''
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)
'''%d '''
num=14
#%d 14
print "num=%d" % num # output: num=14
#%1d 1 , 1 , , %1d 14
print "num=%1d" % num # output: num=14
#%3d 3 , 3 , , %3d 14
print "num=%3d" % num # output: num= 14
#%-3d 3 , 3 , , %3d 14_
print "num=%-3d" % num # output: num=14_
#%05d 5 , 5 , 0, %05d 00014
print "num=%05d" % num # output: num=00014
#%.3d 3 3 ,
# 3 , 0, %.3d 014
print "num=%.3d" % num # output: num=014
#%.0003d 0003 3 , 3, 3 ,
# 3 , 0, %.3d 014
print "num=%.0003d" % num # output: num=014
#%5.3d , 3 , 0, 5 , ,
# 0 , , %5.3d 014
print "num=%5.3d" % num # output: num= 014
#%05.3d , 3 , 0, 5 ,
# 05, 0, , %05.3d 00014
print "num=%05.3d" % num # output: num=00014
# %*.*d , *
# , 04 0 , , 3 0
print "num=%*.*d" % (04,3,num) # output: num= 014
'''%f '''
import math
#%a.bf,a ,b
# %f , 5
print "PI=%f" % math.pi # output: PI=3.141593
# %9f , 9 , ,
print "PI=%9f" % math.pi # output: PI=_3.141593
# . , ,03 3 0
print "PI=%03.f" % math.pi # output: PI=003
#%6.3f 3 , 6 , ,
print "PI=%6.3f" % math.pi # output: PI=_3.142
#%-6.3f 3 , 6 , ,
print "PI=%-6.3f" % math.pi # output: PI=3.142_
# %*.*f , *
# , 06 0 ,
print "PI=%*.*f" % (06,3,math.pi) # output: PI=_3.142
変換元:https://blog.csdn.net/qq_37482544/article/details/63720726