笨办法学python 习题5:更多的变量和打印
程序员文章站
2022-03-04 14:17:21
...
熟悉格式化字符
name = 'Zed A. Shaw'
age = 35
height = 74 # inches(英寸)
height_cm = height * 2.54 #英寸换厘米
weight = 180 # lbs(磅)
weight_kg = weight * 0.45359 #磅换千克
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'
#格式化字符 %s(格式化字符串)替换后面的变量或值 因只有一个变量%后面省略()
print ("Let's talk about %s." % name)
#格式化字符 %d(格式化整数)替换后面的变量或值 因只有一个变量%后面省略()
print ("He's %d inches tall." % height) #他有XX英寸高
print ("He's %d pounds heavy." % weight)#他有XX磅重
#print ("Actually that's not too heavy.") #自黑就不练了
#格式化字符 %s(格式化字符串)替换后面的两个变量或值
print ("He's got %s eyes and %s hair." % (eyes, hair))
#格式化字符 %s(格式化字符串)替换后面的变量或值 因只有一个变量%后面省略()
print ("His teeth are usually %s." % teeth)
#格式化字符 %d(格式化整数)替换后面的变量或值,使用厘米和千克
print ("If I add %dage, %dcm, and %dkg I get %d." % (age, height_cm, weight_kg, age + height_cm + weight_kg))
打印结果