如果我写f,为什么python中的字符串格式不起作用


name = 'peace'
age = 21
print(f"your name is {name} and your age is {age}")

错误文件",第1行f"你的名字是{name},年龄是{age}。">

SyntaxError:语法无效

如果您的Python版本早于3.6,则不能使用f-string,因为它们是在Python 3.6中添加的,但可以使用"{}".format(something)

name = 'peace'
age = 21
print("your name is {} and your age is {}".format(name,age))

另请参阅:PEP 3101——高级字符串格式

最新更新