1. 環(huán)境介紹
我使用的環(huán)境是windows/notepad++/Python 3.7.2
2. 代碼
# coding:utf-8
my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
'''
python2
print"Let's talk about %s." % my_name
'''
# python3
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} deppending 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}.")
3.輸出
ex05輸出.png
4.附加練習
練習1:將代碼中的my_去掉
# coding:utf-8
# 習題1:將my_去掉
name = 'Zed A. Shaw'
age = 35 # not a lie
height = 74 # inches
weight = 180 # lbs
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'
print(f"Let's talk about {name}.")
print (f"He's {height} inches tall.")
print (f"He's {weight} pounds heavy.")
print ("Actually that's not too heavy.")
print (f"He's got {eyes} eyes and {hair} hair.")
print (f"His teeth are usually {teeth} deppending on the coffee.")
# this line is tricky,try to get it exactly right
total = age + height + weight
print(f"if I add {age},{height},and {weight} I get {total}.")
5.總結
- python2和python3中不同的地方展現(xiàn)出來了,敲完代碼后我感覺Python3的邏輯相較于Python2要更好一些。不知道是否是我個人的感受。更加合理了颤芬。
- 變量嵌入字符串中使用中括號{}括起來
- Python3中格式化字符串需要用f進行表示
- 格式化字符串的意義就是將變量放到文字中