python 转字符串
时间: 2023-09-14 17:10:06 浏览: 38
在 Python 中,可以使用 str() 函数将其他数据类型转换为字符串类型。例如:
```
x = 123
y = str(x)
print(y) # 输出:'123'
```
还可以使用 format() 方法将变量插入到字符串中:
```
name = 'Alice'
age = 25
s = 'My name is {} and my age is {}'.format(name, age)
print(s) # 输出:'My name is Alice and my age is 25'
```
或者使用 f-string:
```
name = 'Alice'
age = 25
s = f'My name is {name} and my age is {age}'
print(s) # 输出:'My name is Alice and my age is 25'
```
阅读全文