python将数字转化成字符串
时间: 2023-06-01 12:03:03 浏览: 122
数字转换为字符串
可以使用str()函数将数字转换为字符串,例如:
```python
num = 123
str_num = str(num)
print(str_num) # 输出:'123'
```
还可以使用格式化字符串的方式,将数字和字符串拼接在一起,例如:
```python
num = 123
str_num = f"The number is {num}"
print(str_num) # 输出:'The number is 123'
```
注意,当使用加号(+)将数字和字符串拼接时,需要先将数字转换为字符串,否则会报错,例如:
```python
num = 123
str_num = "The number is " + num # 报错
```
正确的写法是:
```python
num = 123
str_num = "The number is " + str(num)
print(str_num) # 输出:'The number is 123'
```
阅读全文