代码
types_of_people = 10
x2 = "There are {} types of people."
x1 = f"There are {types_of_people} types of people."
binary = "binary"
do_not = "don't"
y2 = "Those who know {} and those who {}."
y1 = f"Those who know {binary} and those who {do_not}."
if ( x1 is x2.format(types_of_people)):
print("0 - yes")
else:
print("0 - no")
if ( y1 == y2.format(binary,"don't")):
print("0 - yes")
else:
print("0 - no")
print("1 - " , x1)
print("2 - " , x2.format(types_of_people))
print("3 - " , y1)
print("4 - " , y2.format(binary,do_not))
print("5 - " , f"I said: {x1}")
print("6 - " , f"I said: {x2.format(types_of_people)}")
print("7 - " , f"I also said: '{y1}'")
print("8 - " , f"I also said: '{y2.format(binary,do_not)}'")
hilarious = "False"
joke_evaluation1 = "Isn't that joke so funny?!{}"
joke_evaluation2 = f"Isn't that joke so funny?!{hilarious}"
print("9 - " , joke_evaluation1.format(hilarious))
print("10 - " , joke_evaluation2)
w = "This is the left side of ..."
e = "a string with a right side."
print(w + e)
遇到问题
好几次把if 和 print,错输入成“If”和“Print”,结果一直提示错误。
学到的
1、如代码所示,a.format(b) == a=f"{b}"(看习题7的“学到的”,这里好像有点不对);
2、print()内的字符串使用“+”连接;
3、小括号“()”内可以使用字符串和值,而大括号“{}”内只能用值。例:y1是y1 = f"Those who know {binary} and those who {do_not}."而y2用法为y2.format(binary,“don’t”))。