python

python print format函数的对齐与宽度设置技巧有哪些

小樊
134
2024-08-10 01:38:36
栏目: 编程语言
Python开发者服务器,限时0元免费领! 查看>>

  1. 左对齐:使用’<’
name = "John"
age = 25
print("{:<10} is {} years old".format(name, age))
  1. 右对齐:使用’>’
name = "John"
age = 25
print("{:>10} is {} years old".format(name, age))
  1. 居中对齐:使用’^’
name = "John"
age = 25
print("{:^10} is {} years old".format(name, age))
  1. 设置宽度:使用数字指定宽度
name = "John"
age = 25
print("{:20} is {} years old".format(name, age))
  1. 设置填充字符:使用指定填充字符
name = "John"
age = 25
print("{:_<10} is {} years old".format(name, age))
  1. 设置精度:使用’.’
pi = 3.14159
print("{:.2f}".format(pi))

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

相关推荐:Python中print的进阶技巧有哪些

0
看了该问题的人还看了