python格式化字符串示例
时间: 2023-10-27 08:49:02 浏览: 121
占位符%字符串格式化输出 – python实例
5星 · 资源好评率100%
1. 使用百分号(%)实现格式化字符串
name = "Tom"
age = 25
print("My name is %s, and I am %d years old." % (name, age))
输出:My name is Tom, and I am 25 years old.
2. 使用format()方法实现格式化字符串
name = "Tom"
age = 25
print("My name is {}, and I am {} years old.".format(name, age))
输出:My name is Tom, and I am 25 years old.
3. 使用f-strings实现格式化字符串
name = "Tom"
age = 25
print(f"My name is {name}, and I am {age} years old.")
输出:My name is Tom, and I am 25 years old.
阅读全文