TypeError: can only concatenate str (not "int") to str
时间: 2023-11-21 12:31:27 浏览: 101
解决Python 异常TypeError: cannot concatenate str and int objects
3星 · 编辑精心推荐
This error occurs when you try to concatenate a string and an integer using the + operator. In Python, you can only concatenate two strings using the + operator, not a string and an integer.
For example, if you have a variable "x" that is an integer and you want to concatenate it with a string "Hello", you will get this error:
x = 5
print("Hello" + x)
To fix this error, you need to convert the integer to a string using the str() function:
x = 5
print("Hello" + str(x))
Output:
Hello5
Now, the integer is converted to a string and can be concatenated with the other string.
阅读全文