To fix “typeerror: can only concatenate str (not “int”) to str” error in Python, there are two solutions we have effectively tested. Follow the article to better understand.
What causes the “typeerror: can only concatenate str (not “int”) to str” error?
The “Typeerror: can only concatenate str (not “int”) to str” error happens when you want to concatenate an integer to a string. If you add real numbers, lists, or other data types to the string, this error will occurs.
Example:
myInfor = { 'name': 'John', 'age': 28 } print(" I am " + myInfor["age"] + ". My name is " + myInfor["name"])
Output:
Traceback (most recent call last):
File "prog.py", line 6, in <module>
print(" I am " + myInfor["age"] + ". My name is " + myInfor["name"])
TypeError: can only concatenate str (not "int") to str
How to solve this error?
Method 1: Use the ‘str’ class and convert an integer to a string
The ‘str’ class is a collection of methods for working with strings.
The str()
function converts an integer to a string and creates a new string.
Syntax:
str(int)
Parameters:
- str: method name
- int: number in int converted to string
The result of the str()
function is a digit in the form of a string.
Example:
myInfor = { 'name': 'John', 'age': 28 } print(" I am " + str(myInfor["age"]) + ". My name is " + myInfor["name"])
Output:
I am 28. My name is John
Method 2: Using formatted string characters (f-string)
F-strings make it easy to embed expressions inside string literals for formatting. In simple terms, we format a string in Python by writing the letter f or F in front of the string and then assigning a replacement value to the replacement field. We then transform the replacement value we just assigned to match the format in the replace field and complete the process.
Example:
name = 'John' age = 28 job = 'Developer' print(f"Hello, My name is {name}. I'm {age} years old. I'm a {job}")
Output:
Hello, My name is John. I'm 28 years old. I'm a Developer
Note:
Enclose expressions in brackets: {expression}
.
If you are unsure about the variable type, use the type()
class.
Example:
name = 'John' print(type(name)) age = 28 print(type(age))
Output:
<class 'str'>
<class 'int'>
Note: The type()
function returns the type of the variable, so you can specify the exact type of the variable to avoid the error.
Summary
If you have any questions about this error “Typeerror: can only concatenate str (not “int”) to str” in Python, leave a comment below. I will answer your questions.
Maybe you are interested:
- TypeError: cannot unpack non-iterable int object in Python
- TypeError: cannot unpack non-iterable NoneType object
- TypeError: Descriptors cannot not be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
- TypeError: int() argument must be a string, a bytes-like object or a number, not ‘list’

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java