`format`函数在Python中是一个字符串格式化方法,它允许你将变量或表达式的值插入到字符串中,并且可以控制插入的格式。`format`函数的基本语法是使用花括号`{}`作为占位符,并通过`format()`方法将值传递给这些占位符。
例如,如果你想将变量`name`和`age`插入到字符串中,你可以这样写:
```python
name = "Alice"
age = 30
formatted_string = "My name is {} and I am {} years old.".format(name, age)
print(formatted_string) 输出:My name is Alice and I am 30 years old.
`format`函数非常灵活,支持位置参数、关键字参数,并且可以指定格式化选项,比如对齐、宽度和精度等。