python中更多变量的打印

python学习的第5课,这次我学会了如何创建嵌入变量内容的字符串。

要在字符串里嵌入变量,需要用到 { },把变量放在里面,字符串要用f开头,f是“格式化”(format)的意思,例如 f"Hello {somevar}",就是把somevar变量里面的东西放到对应字符串中出现的位置。

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'Write'
my_hair = 'Brown'

print(f"Let's talk about {my_name}.")
print(f"He's {my_height} inches tall.")
print(f"He's {my_weight} pounds heavy.")
print("Actually that's not too heavy.")
print(f"He's got {my_eyes} eyes and {my_hair} hair.")
print(f"His teeth are usually {my_teeth} depending on the coffee.")

# this line is tricky, try to get it exactly right
total = my_age + my_height + my_weight
print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}")

python中更多变量的打印_第1张图片

运行结果:
python中更多变量的打印_第2张图片

你可能感兴趣的:(python)