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