python中字符串的格式化。
1、format
位置参数
>>> "{0} and {1}".format("aaa","bbb") 'aaa and bbb' >>> "{0},{1} and {2}".format("aaa","bbb","ccc") 'aaa,bbb and ccc'
关键字参数
>>> "{a},{b} and {c}".format(c = "xxx",a = "mmm",b = "qqq") 'mmm,qqq and xxx'
>>> "{0}:{1:.2f}".format("圆周率:",3.1415926) '圆周率::3.14'
2、格式化操作符 %
格式化字符串:%s
>>> "%s" % "abcd" 'abcd'
格式化整数:%d
>>> "%d" % 100 '100' >>> "%d" % 100.434 '100'
格式化浮点数:%f
>>> "%f" % 100.34245 '100.342450' >>> "%f" % 100 '100.000000'
科学计数法:%e
>>> "%e" % 1000 '1.000000e+03'