Linux 拨号vps windows公众号手机端

python中print格式化输出的方法是什么

lewis 9年前 (2016-10-28) 阅读数 12 #程序编程
文章标签 python

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.")
版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门