Python的format方法是一种字符串格式化的方法,它允许我们将变量或表达式的值插入到字符串中的特定位置。通过使用大括号{}作为占位符,在format方法中传递相应的参数,我们可以动态地构建字符串并将值填充到指定的位置。
下面是一个简单的示例,演示了如何使用format方法来格式化字符串:
name = "Alice"
age = 25
message = "My name is {} and I'm {} years old.".format(name, age)
print(message)
输出结果将是:
My name is Alice and I'm 25 years old.
<