Python中format的用法
#format的用法
#d表示整数数据类型
x = 4
y = 5
z = x + y
print("Output #2: Four plus five equals {0:d}.".format(z))
'''
{0:d}表示我们需要输出的值,0表示format中第一个未知数,d则表示数据形式为整数
'''
'''
Output #2: Four plus five equals 9.
'''
#f表示浮点数数据类型
from math import exp, log, sqrt
print("Output #11: {0:.4f}".format(exp(3)))
print("Output #12: {0:.2f}".format(log(4)))
print("Output #13: {0:.1f}".format(sqrt(81)))
'''
Output #11: 20.0855
Output #12: 1.39
Output #13: 9.0
'''
#s表示字符串数据类型
string1 = "This is a "
string2 = "short string."
sentence = string1 + string2
print("Output #18: {0:s}".format(sentence))
print("Output #19: {0:s} {1:s}{2:s}".format("She is", "very "*4, "beautiful."))
m = len(sentence)
print("Output #20: {0:d}".format(m))
'''
Output #18: This is a short string.
Output #19: She is very very very very beautiful.
Output #20: 23
'''