新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

python中print格式化输出的有哪些方法,python的print

发布时间:2024-03-21 20:06:50

python中print格式化输出的有哪些方法

Python中print函数的格式化输出可以通过占位符的方式实现。经常使用的占位符有:

  • %s:字符串
  • %d:整数
  • %f:浮点数

示例以下:

name = "Alice"
age = 20
height = 1.65
print("Hello, my name is %s, I am %d years old and %.2f meters tall." % (name, age, height))

另外,Python3.6及以上版本还支持使用f字符串进行格式化输出,示例以下:

name = "Alice"
age = 20
height = 1.65
print(f"Hello, my name is {name}, I am {age} years old and {height:.2f} meters tall.")