Last updated: Apr 8, 2024
Reading timeยท19 min
The Python "TypeError: can only concatenate str (not "int") to str" occurs when we try to concatenate a string and an integer.
To solve the error, convert the int
to a string, e.g. str(my_int)
to
concatenate the strings or convert the str to an int, e.g. int(my_str)
to add
the numbers.
Here is an example of how the error occurs.
# โ๏ธ TypeError: can only concatenate str (not "int") to str result = 'hi' + 123
We tried to use the addition (+) operator to concatenate a string and an integer which caused the error.
The values on the left and right-hand sides need to be of compatible types.
One way to solve the error is to convert the integer to a string.
result = 'hi' + str(123) print(result) # ๐๏ธ 'hi123'
We passed the integer to the str() class and converted it to a string before concatenating the two strings.
If you have a number that is wrapped in a string and an integer, you need to convert the string to an integer (or float) to add the two numbers.
result = int('50') + 50 print(result) # ๐๏ธ 100
We passed the string to the int() class to convert it to an integer. Note that you can also use the float() class if you need to convert a string to a floating-point number.
input()
built-in function, all of the values the user enters get converted to strings (even numeric values).An alternative to concatenating strings with the addition (+) operator is to use a formatted string literal.
str_1 = 'abc' num_1 = 123 result = f'{str_1} {num_1} def' print(result) # ๐๏ธ 'abc 123 def'
f
.Make sure to wrap expressions in curly braces - {expression}
.
If you aren't sure what type a variable stores, use the built-in type()
class.
str_1 = 'abc' print(type(str_1)) # <class 'str'> print(isinstance(str_1, str)) # ๐๏ธ True num_1 = 123 print(type(num_1)) # <class 'int'> print(isinstance(num_1, int)) # ๐๏ธ True
The type class returns the type of an object.
input()
function always returns a stringThe error often occurs when getting user input using the built-in input()
function.
num = input('Enter your fav number: ') print(num) # ๐๏ธ '10' print(type(num)) # ๐๏ธ <class 'str'> result = num + 100 # โ๏ธ TypeError: can only concatenate str (not "int") to str print(result)
The input() function converts the data to a string and returns it.
input()
.If you need to convert the result to a number, pass it to the int()
(or
float()
) class.
num = input('Enter your fav number: ') print(num) # ๐๏ธ '10' print(type(num)) # ๐๏ธ <class 'str'> # โ convert to int result = int(num) + 100 print(result) # ๐๏ธ 110
We converted the string we got from the input
function back to an integer and
added the two numbers.
The values we are adding with the addition (+) operator must be of compatible types, e.g. a float and an int.
We can't simply add a string and an integer using the addition (+) operator as that causes the error.
The error also occurs when formatting dictionary or list values of type int
to
a string.
employee = { "id": 1, "name": "Bobby Hadz", "age": 30, } # โ๏ธ TypeError: can only concatenate str (not "int") to str result = 'Employee id: ' + employee['id']
The id
key in the employee
dictionary stores an integer, so we can't use the
addition (+) operator with a string and an integer.
You can use a formatted string literal to concatenate the two values without the need to convert the integer to a string.
employee = { "id": 1, "name": "Bobby Hadz", "age": 30, } result = f"Employee id: {employee['id']}" print(result) # ๐๏ธ Employee id: 1
The f-string automatically converts the integer to a string under the hood so we can concatenate the two values directly.
The Python "TypeError: can only concatenate str (not "list") to str" occurs when we try to concatenate a string and a list.
To solve the error, access the list at a specific index to concatenate two
strings, or use the append()
method to add an item to the list.
Here is an example of how the error occurs.
my_str = 'fruits: ' my_list = ['apple', 'banana', 'kiwi'] # โ๏ธ TypeError: can only concatenate str (not "list") to str print(my_str + my_list)
The values on the left and right-hand sides need to be of compatible types.
If you only need to print the contents of the list, use a comma between the string and the list.
my_str = 'fruits: ' my_list = ['apple', 'banana', 'kiwi'] print(my_str, my_list) # ๐๏ธ fruits: ['apple', 'banana', 'kiwi']
If you meant to concatenate the string and a specific item of the list, access the list at the specific index.
my_str = 'fruit: ' my_list = ['apple', 'banana', 'kiwi'] print(my_str + my_list[0]) # ๐๏ธ fruit: apple
We accessed the list item at index 0
, which is a string, so we were able to
concatenate the two strings.
If the item isn't of type string, use the str()
class to convert it to one.
my_str = 'number: ' my_list = [1, 2, 3] print(my_str + str(my_list[0])) # ๐๏ธ number: 1
If you need to add an item to a list, use the append()
method.
my_str = 'melon' my_list = ['apple', 'banana', 'kiwi'] my_list.append(my_str) print(my_list) # ๐๏ธ ['apple', 'banana', 'kiwi', 'melon']
The list.append() method adds an item to the end of the list.
The method returns None as it mutates the original list.
The error is often caused when you have a two-dimensional list.
my_str = 'fruit: ' my_list = [['apple'], ['banana'], ['kiwi']] # โ๏ธ TypeError: can only concatenate str (not "list") to str result = my_str + my_list[0]
We accessed the list at index 0
, but because we have a two-dimensional list,
the list element at index 0
is another list.
If we need to access an item in the nested list, we have to access it at its specific index.
my_str = 'fruit: ' my_list = [['apple'], ['banana'], ['kiwi']] result = my_str + my_list[0][0] print(result) # ๐๏ธ fruit: apple
We accessed the nested list at index 0
and concatenated the two strings.
If you aren't sure what type a variable stores, use the built-in type()
class.
my_str = 'hello world' print(type(my_str)) # ๐๏ธ <class 'str'> print(isinstance(my_str, str)) # ๐๏ธ True my_list = ['apple', 'banana'] print(type(my_list)) # ๐๏ธ <class 'list'> print(isinstance(my_list, list)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.
If you need to print the contents of a list and a string, you can also use a formatted string literal.
my_str = 'fruits:' my_list = ['apple', 'banana'] result = f'{my_str} {my_list}' print(result) # ๐๏ธ fruits: ['apple', 'banana']
f
.Make sure to wrap expressions in curly braces - {expression}
.
The Python "TypeError: can only concatenate str (not "NoneType") to str"
occurs when we try to concatenate a string and a None
value.
To solve the error, correct the assignment or check if the variable doesn't
store a None
value before concatenating.
Here is an example of how the error occurs.
example = None # โ๏ธ TypeError: can only concatenate str (not "NoneType") to str result = 'hello ' + example
We tried to use the addition (+) operator to concatenate a string and a None value which caused the error.
None
value and correct the assignment or check if the variable doesn't store None
before concatenating.The most common sources of a None
value are:
None
implicitly).None
.None
Here is an example of getting a None
value from a function that doesn't return
anything (implicitly returns None
).
def get_name(): print('Bobby Hadz') # โ๏ธ TypeError: can only concatenate str (not "NoneType") to str result = 'hello ' + get_name()
The get_name
function doesn't return anything, so it implicitly returns
None
.
You can use a return statement to return a value from the function.
def get_name(): return 'Bobby Hadz' result = 'hello ' + get_name() print(result) # ๐๏ธ "hello Bobby Hadz"
You can use an if
statement if you only want to concatenate the values if the
variable doesn't store None
.
name = None if name is not None: result = 'hello ' + name print(result) else: # ๐๏ธ this runs print('variable stores a None value')
The if
block is only run if the name
variable doesn't store a None
value,
otherwise the else
block is run.
None
Alternatively, you can assign a fallback value to the variable if it is None
.
name = None if name is None: name = '' result = 'hello ' + name print(result) # ๐๏ธ 'hello'
We
check if the name
variable stores a None value,
and if it does, we set it to an empty string.
Note that if you use a
formatted string literal with
an expression that returns None
, the None
value will be included in the
string.
name = None result = f'hello {name}' print(result) # ๐๏ธ 'hello None'
f
.Make sure to wrap expressions in curly braces - {expression}
.
Note that many built-in methods mutate the original object (e.g. sort()
) and
therefore don't return anything (implicitly return None
), so make sure you
aren't storing the result of calling one in a variable.
Another common source of None
values is having a function that only returns a
value if a certain condition is met.
def get_name(a): if len(a) > 3: return a result = get_name('Bob') print(result) # ๐๏ธ None
The if
statement in the get_name
function is only run if the passed-in
argument has a length greater than 3
.
None
.To solve the error in this scenario, you either have to check if the function
didn't return None
, or return a default value if the condition is not met.
def get_name(a): if len(a) > 3: return a return '' # ๐๏ธ return an empty string if condition not met result = get_name('Bob') print(result) # ๐๏ธ ""
Now the function is guaranteed to return a value regardless if the condition is met.
The Python "TypeError: can only concatenate str (not "bytes") to str" occurs when we try to concatenate a string and a bytes object.
To solve the error, decode the bytes object into a string before concatenating the strings.
Here is an example of how the error occurs.
my_str = 'hello ' my_bytes = b'Bobby Hadz' # โ๏ธ TypeError: can only concatenate str (not "bytes") to str # TypeError: can't concat str to bytes result = my_str + my_bytes
The values on the left and right-hand sides need to be of compatible types.
One way to solve the error is to convert the bytes object to a string.
my_str = 'hello ' my_bytes = b'Bobby Hadz' result = my_str + my_bytes.decode('utf-8') print(result) # ๐๏ธ 'hello Bobby Hadz'
The bytes.decode() method returns a
string decoded from the given bytes. The default encoding is utf-8
.
Alternatively, you can encode the string to a bytes object.
my_str = 'hello ' my_bytes = b'Bobby Hadz' result = my_str.encode('utf-8') + my_bytes print(result) # ๐๏ธ b'hello Bobby Hadz'
The str.encode() method returns an
encoded version of the string as a bytes object. The default encoding is
utf-8
.
Once you convert the bytes object to a string, you can use formatted string literals.
my_str = 'hello' # ๐๏ธ decode bytes to str my_bytes = b'Bobby Hadz'.decode('utf-8') result = f'{my_str} {my_bytes}' print(result) # ๐๏ธ "hello Bobby Hadz"
f
.Make sure to wrap expressions in curly braces - {expression}
.
If you aren't sure what type a variable stores, use the built-in type()
class.
my_str = 'hello' print(type(my_str)) # ๐๏ธ <class 'str'> print(isinstance(my_str, str)) # ๐๏ธ True my_bytes = b'Bobby Hadz' print(type(my_bytes)) # ๐๏ธ <class 'bytes'> print(isinstance(my_bytes, bytes)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance function returns
True
if the passed-in object is an instance or a subclass of the passed-in
class.
The Python "TypeError: can only concatenate str (not "float") to str" occurs when we try to concatenate a string and a float.
To solve the error, convert the float to a string, e.g. str(my_float)
to
concatenate the strings or convert the str to a float, e.g. float(my_str)
.
Here is an example of how the error occurs.
# โ๏ธ TypeError: can only concatenate str (not "float") to str result = 'it costs: ' + 59.49
The values on the left and right-hand sides need to be of compatible types.
One way to solve the error is to convert the float to a string.
result = 'it costs: ' + str(59.49) print(result) # ๐๏ธ it costs: 59.49
We passed the floating-point number to the str()
class and converted it to a
string before concatenating the two strings.
If you have a number that is wrapped in a string and a float, you need to convert the string to a float (or an integer) to add the two numbers.
result = float('10.51') + 59.49 print(result) # ๐๏ธ 70.0
We passed the string to the float()
class to convert it to a floating-point
number. Note that you can also use the int()
class if you need to convert a
string to an integer.
input()
built-in function, all of the values the user enters get converted to strings (even numeric values).An alternative to concatenating strings with the addition (+) operator is to use a formatted string literal.
str_1 = 'it costs: ' num_1 = 59.49 result = f'{str_1} {num_1} usd' print(result) # ๐๏ธ 'it costs: 59.49 usd'
f
.Make sure to wrap expressions in curly braces - {expression}
.
If you aren't sure what type a variable stores, use the built-in type()
class.
str_1 = 'it costs: ' print(type(str_1)) # ๐๏ธ <class 'str'> print(isinstance(str_1, str)) # ๐๏ธ True num_1 = 59.49 print(type(num_1)) # ๐๏ธ <class 'float'> print(isinstance(num_1, float)) # ๐๏ธ True
The type class returns the type of an object.
The error often occurs when getting user input using the built-in input()
function.
num = input('Enter your fav number: ') print(num) # ๐๏ธ '13.7' print(type(num)) # ๐๏ธ <class 'str'> result = num + 16.3 # โ๏ธ TypeError: can only concatenate str (not "float") to str print(result)
input
string to a floatThe input() function converts the data to a string and returns it.
input()
.If you need to convert the result to a number, pass it to the float()
(or
int()
) class.
num = input('Enter your fav number: ') print(num) # ๐๏ธ '13.7' print(type(num)) # ๐๏ธ <class 'str'> # โ convert to float result = float(num) + 16.3 print(result) # ๐๏ธ 30.0
We converted the string we got from the input
function back to a float and
added the two numbers.
The values we are adding with the addition (+) operator must be of compatible types, e.g. a float and an int.
We can't simply add a string and a float using the addition (+) operator as that causes the error.
The Python "TypeError: can only concatenate str (not "dict") to str" occurs when we try to concatenate a string and a dictionary.
To solve the error, convert the dictionary to a string or access a specific key of the dictionary.
Here is an example of how the error occurs.
my_str = 'employee: ' my_dict = {'name': 'Bobby Hadz', 'age': 30} # โ๏ธ TypeError: can only concatenate str (not "dict") to str print(my_str + my_dict)
The values on the left and right-hand sides need to be of compatible types.
If you only need to print the contents of the dictionary, use a comma between the string and the dictionary.
my_str = 'employee: ' my_dict = {'name': 'Bobby Hadz', 'age': 30} # ๐๏ธ employee: {'name': 'Bobby Hadz', 'age': 30} print(my_str, my_dict)
Alternatively, you can use a formatted string literal.
my_str = 'employee:' my_dict = {'name': 'Bobby Hadz', 'age': 30} result = f'{my_str} {my_dict}' # ๐๏ธ employee: {'name': 'Bobby Hadz', 'age': 30} print(result)
f
.Make sure to wrap expressions in curly braces - {expression}
.
If you meant to concatenate the string and the value of a specific key in the
dict
, access the key using square brackets.
my_str = 'employee: ' my_dict = {'name': 'Bobby Hadz', 'age': 30} result = my_str + my_dict['name'] # ๐๏ธ employee: Bobby Hadz print(result)
We accessed the name
key of the dictionary, and since the value for the key is
a string, we were able to concatenate the two strings.
If the value is of type number, convert it to a string before concatenating.
my_str = 'employee age: ' my_dict = {'name': 'Bobby Hadz', 'age': 30} result = my_str + str(my_dict['age']) # ๐๏ธ employee age: 30 print(result)
You can also convert the dict to a string by passing it to the str()
class.
my_str = 'employee: ' my_dict = {'name': 'Bobby Hadz', 'age': 30} result = my_str + str(my_dict) # ๐๏ธ employee: {'name': 'Bobby Hadz', 'age': 30} print(result)
If you aren't sure what type a variable stores, use the built-in type()
class.
my_str = 'employee: ' print(type(my_str)) # ๐๏ธ <class 'str'> print(isinstance(my_str, str)) # ๐๏ธ True my_dict = {'name': 'Alice', 'age': 30} print(type(my_dict)) # ๐๏ธ <class 'dict'> print(isinstance(my_dict, dict)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance function returns
True
if the passed-in object is an instance or a subclass of the passed-in
class.
The Python "TypeError: can only concatenate str (not "numpy.float64") to str" occurs when we try to concatenate a string and a numpy float.
To solve the error, convert the numpy float to a string, e.g. str(my_float)
to concatenate the strings.
Here is an example of how the error occurs.
import numpy as np my_float = np.power(13.3, 3, dtype=np.float64) # โ๏ธ TypeError: can only concatenate str (not "numpy.float64") to str result = 'It costs: ' + my_float
The values on the left and right-hand sides need to be of compatible types.
One way to solve the error is to convert the NumPy float to a string.
import numpy as np my_float = np.power(13.3, 3, dtype=np.float64) result = 'It costs: ' + str(my_float) print(result) # ๐ It costs: 2352.637
We passed the NumPy floating-point number to the str()
class and converted it
to a string before concatenating the two strings.
If you have a number that is wrapped in a string and a float, you need to convert the string to a float (or an integer) to add the two numbers.
import numpy as np my_float = np.power(13.3, 3, dtype=np.float64) result = float('10.3') + my_float # ๐๏ธ or use np.float64 # result = np.float64('10.3') + my_float print(result) # ๐ 2362.9370000000004
We passed the string to the float()
class to convert it to a floating-point
number. Note that you can also use the int()
class if you need to convert a
string to an integer.
input()
built-in function, all of the values the user enters get converted to strings (even numeric values).An alternative to concatenating strings with the addition (+) operator is to use a formatted string literal.
import numpy as np str_1 = 'it costs: ' num_1 = np.power(13.3, 3, dtype=np.float64) result = f'{str_1} {num_1} usd' print(result) # ๐๏ธ 'it costs: 2352.637 usd'
f
.Make sure to wrap expressions in curly braces - {expression}
.
If you aren't sure what type a variable stores, use the built-in type()
class.
import numpy as np str_1 = 'it costs: ' print(type(str_1)) # ๐๏ธ <class 'str'> print(isinstance(str_1, str)) # ๐๏ธ True num_1 = np.power(13.3, 3, dtype=np.float64) print(type(num_1)) # ๐๏ธ <class 'numpy.float64'> print(isinstance(num_1, np.float64)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance function returns
True
if the passed-in object is an instance or a subclass of the passed-in
class.
The Python "TypeError: can only concatenate str (not "set") to str" occurs when we try to concatenate a string and a set.
To solve the error, use a formatted string literal, or use the add()
method
to add an item to the set.
Here is an example of how the error occurs.
my_str = 'fruits: ' my_set = {'apple', 'banana', 'kiwi'} # โ๏ธ TypeError: can only concatenate str (not "set") to str print(my_str + my_set)
The values on the left and right-hand sides need to be of compatible types.
set
If you only need to print the contents of the set, use a comma between the string and the set.
my_str = 'fruits: ' my_set = {'apple', 'banana', 'kiwi'} # ๐๏ธ fruits: {'apple', 'kiwi', 'banana'} print(my_str, my_set)
Alternatively, you can use a formatted string literal.
my_str = 'fruits:' my_set = {'apple', 'banana', 'kiwi'} result = f'{my_str} {my_set}' print(result) # ๐๏ธ fruits: {'kiwi', 'apple', 'banana'}
f
.Make sure to wrap expressions in curly braces - {expression}
.
set
If you need to add an item to the set, use the add()
method.
my_str = 'fruits: ' my_set = {'apple', 'banana', 'kiwi'} my_set.add('melon') # ๐๏ธ {'kiwi', 'banana', 'apple', 'melon'} print(my_set)
The set.add()
method adds the provided element to the set
.
You can also pass the set
to the str()
class to convert it to a string
before concatenating the two strings.
my_str = 'fruits: ' my_set = {'apple', 'banana', 'kiwi'} result = my_str + str(my_set) # ๐๏ธ fruits: {'kiwi', 'apple', 'banana'} print(result)
If you aren't sure what type a variable stores, use the built-in type()
class.
my_str = 'fruits: ' print(type(my_str)) # ๐๏ธ <class 'str'> print(isinstance(my_str, str)) # ๐๏ธ True my_set = {'apple', 'banana', 'kiwi'} print(type(my_set)) # ๐๏ธ <class 'set'> print(isinstance(my_set, set)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance function returns
True
if the passed-in object is an instance or a subclass of the passed-in
class.
The Python "TypeError: Can only concatenate str (not "numpy.int64") to str" occurs when we try to concatenate a string and a numpy int.
To solve the error, convert the numpy int to a string, e.g.
str(my_numpy_int)
to concatenate the strings.
Here is an example of how the error occurs.
import numpy as np str_1 = 'it costs: ' num_1 = np.power(10, 3, dtype=np.int64) # โ๏ธ TypeError: can only concatenate str (not "numpy.int64") to str result = str_1 + num_1
The values on the left and right-hand sides need to be of compatible types.
One way to solve the error is to convert the NumPy int to a string.
import numpy as np str_1 = 'it costs: ' num_1 = np.power(10, 3, dtype=np.int64) # ๐๏ธ convert to str result = str_1 + str(num_1) print(result) # ๐๏ธ 'it costs: 1000'
We passed the NumPy integer to the str()
class and converted it to a string
before concatenating the two strings.
If you have a number that is wrapped in a string and an integer, you need to convert the string to an integer (or float) to add the two numbers.
import numpy as np str_1 = '500' num_1 = np.power(10, 3, dtype=np.int64) result = int(str_1) + num_1 print(result) # ๐๏ธ 1500
We passed the string to the int()
class to convert it to an integer. Note that
you can also use the float()
class if you need to convert a string to a
floating-point number.
input()
built-in function, all of the values the user enters get converted to strings (even numeric values).An alternative to concatenating strings with the addition (+) operator is to use a formatted string literal.
import numpy as np str_1 = 'it costs' num_1 = np.power(10, 3, dtype=np.int64) result = f'{str_1} {num_1} usd' print(result) # ๐๏ธ 'it costs 1000 usd'
f
.Make sure to wrap expressions in curly braces - {expression}
.
If you aren't sure what type a variable stores, use the built-in type()
class.
import numpy as np str_1 = 'it costs' print(type(str_1)) # ๐๏ธ <class 'str'> print(isinstance(str_1, str)) # ๐๏ธ True num_1 = np.power(10, 3, dtype=np.int64) print(type(num_1)) # ๐๏ธ <class 'numpy.int64'> print(isinstance(num_1, np.int64)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.