Python中的print格式化用于將變量的值插入到字符串中,以便在輸出時顯示。它有以下幾種用法:
name = "Alice"
age = 25
print("My name is %s and my age is %d" % (name, age))
name = "Alice"
age = 25
print("My name is {} and my age is {}".format(name, age))
name = "Alice"
age = 25
print(f"My name is {name} and my age is {age}")
這些方法都可以用來格式化打印輸出,根據具體的需求選擇適合的方法。