温馨提示×

python的print()函数如何进行格式化输出

小樊
83
2024-08-27 09:13:51
栏目: 编程语言

Python 中的 print() 函数可以使用 format() 方法或者 f-string(在 Python 3.6 及更高版本中)进行格式化输出。

  1. 使用 format() 方法:
name = "张三"
age = 25
print("我的名字是{},我今年{}岁。".format(name, age))
  1. 使用 f-string:
name = "张三"
age = 25
print(f"我的名字是{name},我今年{age}岁。")

这两种方法都可以实现格式化输出,但是 f-string 更简洁,推荐使用。

0