28 - 使用fstring 方法格式化字符串

1. 在Python语言中哪种格式化方法可以直接使用变量

  • fstring
  • fstring 方式是指在字符串中直接使用Python 变量,这需要在字符串前面用f 标注

2. 请用代码描述如何使用fstring格式化字符串

# fstring

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
    def getAge(self):
        return 41

person = Person()

s1 = f'我是{name}, 我今年{person.age1}岁, 明年{person.getAge()}岁'
print(s1)
我是Bill, 我今年40岁, 明年41岁

持续更新中。。。。

发布了123 篇原创文章 · 获赞 120 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_29339467/article/details/104358970
今日推荐