知识点:了解字符串,初次接触str.format方法
之前的练习一直在写字符串(strings),可能你还不知道它们有什么用。现在我们加强练习来弄明白它们是干什么的。
首先解释一下:字符串是你想给某人看的信息,或者你想从程序里输出一些东西。只要放在单引号或双引号里通过 print 就能输出来。我们之前使用 f"blahblah{变量}" 把需要的变量放进去。
python还有另一个格式: .format(), 我们接下来的练习中会用到。
types_of_people = 10
x = f"There are {types_of_people} types of people."
binary = "binary" # 二进制
do_not = "don't"
y = f"Those who know {binary} and those who {do_not}."
print(x)
print(y)
print(f"I said: {x}")
print(f"I also said: '{y}'")
hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}"
print(joke_evaluation.format(hilarious))
# 我们看到的更多的是下面的写法
# print("Isn't that joke so funny?! {}".format(hilarious))
w = "This is the left side of ..."
e = "a string with a right side."
print(w + e) # 将两句用 + 号连起来
What you should see
There are 10 types of people.
Those who know binary and those who don't.
I said: There are 10 types of people.
I also said: 'Those who know binary and those who don't.'
Isn't that joke so funny?! False
This is the left side of ...a string with a right side.
Stuey Drills
1、为每行添加注释
2、找出所有 字符串含字符串 的位置,总共有4处
3、你确定只有4处吗?我可能是骗你的
4、解释一下最后一行。 + 号的用法