在Python中,有几种方式来格式化prin`函数的输出。以下是一些常见的方法:
1. 使用百分号 % 运算符(传统方法):
使用 % 操作符来进行格式化是一种较老的方法,但它仍然有效。
name = "Alice"
age = 25
print("Hello, %s. You are %d years old." % (name, age))
2. 使用`str.format()`方法:
str.format() 提供了一种更现代、更灵活的方式来格式化字符串。
name = "Alice"
age = 25
print("Hello, {}. You are {} years old.".format(name, age))
# 或者使用索引指定顺序:
print("Hello, {0}. You are {1} years old.".format(name, age))

订阅专栏 解锁全文
5万+

被折叠的 条评论
为什么被折叠?



