习题 6:字符串和文本


虽然你已经在程序中写过字符串了,但你还不了解它。

字符串通常是指你想要展示给别人的或者想要从程序里“导出”的一小段字符。Python可以通过文本里的双引号(")或者单引号(')识别出字符串来。

字符串可以包含之前已经见过的格式化字符。变量是你用“名字 = 值”这样的代码设置出来的。

Python还有一种使用 .format( ) 语法的格式化方式,你会在ex6.py发现它。


 ex6.py

# 人的类型
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)
# 我说:有10种类型的人。
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."

# 这是左边的...右边的字符串。
print(w + e)

 结果:

原文地址:https://www.cnblogs.com/llr211/p/11453185.html