1. 使用f-string(格式化字符串字面值):
```python
name = "Alice"
age = 30
sentence = f"My name is {name} and I am {age} years old."
print(sentence) 输出:My name is Alice and I am 30 years old.
2. 使用`str.format()`方法:
```python
name = "Alice"
age = 30
sentence = "My name is {} and I am {} years old.".format(name, age)
print(sentence) 输出:My name is Alice and I am 30 years old.
3. 使用旧式的百分号格式化(`%`符号):
```python
s = "Hello"
x = len(s)
print("The length of %s is %d" % (s, x)) 输出:The length of Hello is 5
以上三种方法都可以将变量的值插入到字符串中。选择哪种方法取决于你的个人喜好和代码的可读性