python

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

小樊
99
2024-08-10 01:38:36
栏目: 编程语言

  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))

0
看了该问题的人还看了