学习目标:
①:使用fstring方式格式化字符串
- Python日常
内容展示:
①:使用fstring方式格式化字符串
# 2
name = 'Bill'
age = 20
def getAge():
return 21
s = f'我是{name}, 今年{age}嵗了,明年{getAge()}'
print(s)
# 我是Bill, 今年20嵗了,明年21
class Person:
def __init__(self):
self.name = 'Mike'
self.age1 = 40
self.age2 = 41
def getAge(self):
return 41
person = Person()
s1 = f'我是{person.name},今年{person.age1},明年{person.age2},明年{person.getAge()}'
print(s1)
# 我是Mike,今年40,明年41,明年41
总结:
1.
fstring方式是指在字符串中直接使用python变量,这需要在字符串前面用 f 标注
- 优快云 Python日常 第【3】2 篇
- 下一篇链接