Python变量中的几种基本类型我们在前面的习题中都已经见过了,4种基本类型:整数类型、浮点类型、字符串类型、布尔类型。下面这道习题中,依旧会看到ex5中出现的“f-string”这种字符串类型,其语法格式为:f"Hello {var}"
,我想你们应该还记得。
下面这道习题把ex5中print函数中的f-string单独抽离出来了,并且赋值给一个变量。另外还有format函数的使用,这是一种针对于字符串格式化的函数。需要注意到最后一条语句是字符串的拼接,可以让短字符串拼接成一个更长的字符串。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
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 wo {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."
print(w + e)
运行结果如下图所示:
ex6_运行结果小结
- 再次认识f-string。
- 字符串的format函数。
- 字符串的拼接。
网友评论