name = "Alice"
age = 25
print("My name is %s and I am %d years old." % (name, age))
name = "Bob"
age = 30
print("My name is {} and I am {} years old.".format(name, age))
print("My name is {0} and I am {1} years old. I am also {1} years of experience in programming.".format(name, age))
pi = 3.1415926
print("The value of pi is approximately {:.2f}".format(pi))
name = "Charlie"
print("|{:<10}|".format(name)) # 左对齐,宽度为 10
print("|{:>10}|".format(name)) # 右对齐,宽度为 10
print("|{:^10}|".format(name)) # 居中对齐,宽度为 10
print("|{:*^10}|".format(name)) # 居中对齐,并用 * 填充
from datetime import datetime
now = datetime.now()
print("Today is {:%Y-%m-%d %H:%M:%S}".format(now))
可以使用特定的格式代码来格式化日期和时间。
本文部分代码转自:https://www.wodianping.com/app/2024-10/50723.html