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}")
这些方法都可以用来格式化打印输出,根据具体的需求选择适合的方法。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:python print format函数的用法及参数说明是什么