python怎么将多个变量加入语句print出
时间: 2024-09-12 12:02:18 浏览: 34
语句-基于智能手机的惯性导航轨迹生成算法
在Python中,你可以使用逗号 `,` 来连接多个变量,并一起打印出来。例如:
```python
name = "Alice"
age = 25
city = "New York"
print("My name is", name, ", I am", age, "years old, and I live in", city)
```
这将输出类似这样的结果:
```
My name is Alice, I am 25 years old, and I live in New York
```
如果你需要在同一行内显示这些信息,可以考虑使用 `f-string` 或者 `str.format()` 方法:
```python
name = "Alice"
age = 25
city = "New York"
# 使用 f-string
print(f"My name is {name}, I am {age} years old, and I live in {city}")
# 或者使用 str.format()
print("My name is {} , I am {} years old, and I live in {}".format(name, age, city))
```
阅读全文