lesson 6 字符串和文本
从今天开始打算每天学习《learn python3 the hard way》,并在博客里记录笔记,希望能坚持下来,总共有52课,我记得,从第六课开始,以下是我今天学习到的:
源代码:
types_of_people = 10
x = f'There are {types_of_people}types_of_people.'
binary = "binary"
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))
w = "This is the left side of ..."
e = "a string with a right side."
-
f+间接打印 先给变量赋值,再用f" “内写入格式化的文本,再将变量插入所需位置(可插入多个变量),再将语句赋值给变量3,最后打印变量3。
f”{变量1}",f"{变量1},{变量2}
举个栗子:
years_old = 18 x = f"I am {years_old} years old." print(x)
-
f+直接打印 直接在print语句中插入变量
举个栗子:
years_old = 18 print(f"I am {years_old} years old.")
-
format+打印 定义一个变量1,再另一个语句里留空的大括号赋值给变量2,打印时候利用.format()打印出来。
连起来其实就是字符串用"{}"挖个坑.format(变量2)
再举个栗子
years_old = 18 x = "I am {} years old" print(x.format(years_old))