Python中的f字符串的用法

Python中的f字符串的用法:要在字符串中插入变量的值,可在前引号前加上字母f,再将要插入的变量放在花括号内。

举例子如下:

first_name="ada"

last_name="lovelace"

full_name=f"{first_name}{last_name}"

print(f"Hello,{full_name.title()}!")

打印结果为:

Hello,Ada Lovelace!


还可以使用f字符串来创建消息,再把整条消息赋给变量:

举例子:

first_name="ada"

last_name="lovelace"

full_name=f"{first_name}{last_name}"

message=f"Hello,{full_name.title()}!"

print(message)

打印结果为:

Hello,Ada Lovelace!

猜你喜欢

转载自blog.csdn.net/liu_hong_yan/article/details/110823236