Python中print函數(shù)的格式化輸出可以通過占位符的方式實現(xiàn)。常用的占位符有:
示例如下:
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.")