在Python中,格式化输出可以通过多种方式实现,以下是几种常见的方法:
1. 使用`%`操作符:
name = "Aviad"
age = 25
print("His name is %s, he is %d years old, and his height is %.2f m." % (name, age, 1.83))
2. 使用`str.format()`方法:
name = "Aviad"
age = 25
height = 1.83
print("His name is {}, he is {} years old, and his height is {:.2f} m.".format(name, age, height))
3. 使用f-string(Python 3.6及以上版本支持):
name = "Aviad"
age = 25
height = 1.83
print(f"His name is {name}, he is {age} years old, and his height is {height:.2f} m.")