TypeError: can only concatenate str (not "X") to str [Fixed]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
19 min

banner

# Table of Contents

  1. Can only concatenate str (not "INT") to str
  2. Can only concatenate str (not "LIST") to str
  3. Can only concatenate str (not "NoneType") to str
  4. Can only concatenate str (not "BYTES") to str
  5. Can only concatenate str (not "FLOAT") to str
  6. Can only concatenate str (not "DICT") to str
  7. Can only concatenate str (not "NUMPY.FLOAT64") to str
  8. Can only concatenate str (not "SET") to str
  9. Can only concatenate str (not "NUMPY.INT64") to str
Make sure to click on the correct subheading depending on your error message.

# TypeError: can only concatenate str (not "int") to str

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.

typeerror can only concatenate str not int to str

Here is an example of how the error occurs.

main.py
# โ›”๏ธ 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.

# Convert the integer to a string to concatenate the string

One way to solve the error is to convert the integer to a string.

main.py
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.

# Convert the string to an integer to add the numbers

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.

main.py
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.

IMPORTANT: if you use the input() built-in function, all of the values the user enters get converted to strings (even numeric values).

# Using a formatted-string literal to concatenate strings

An alternative to concatenating strings with the addition (+) operator is to use a formatted string literal.

main.py
str_1 = 'abc' num_1 = 123 result = f'{str_1} {num_1} def' print(result) # ๐Ÿ‘‰๏ธ 'abc 123 def'
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with 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.

main.py
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.

# The input() function always returns a string

The error often occurs when getting user input using the built-in input() function.

main.py
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.

Even if the user enters a number, it gets converted to a string before it is returned from input().

If you need to convert the result to a number, pass it to the int() (or float()) class.

main.py
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.

# Avoid the error when accessing dict or list values

The error also occurs when formatting dictionary or list values of type int to a string.

main.py
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.

main.py
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.

# Table of Contents

  1. Can only concatenate str (not "LIST") to str
  2. Can only concatenate str (not "NoneType") to str
  3. Can only concatenate str (not "BYTES") to str
  4. Can only concatenate str (not "FLOAT") to str
  5. Can only concatenate str (not "DICT") to str
  6. Can only concatenate str (not "NUMPY.FLOAT64") to str
  7. Can only concatenate str (not "SET") to str
  8. Can only concatenate str (not "NUMPY.INT64") to str

# TypeError: can only concatenate str (not "list") to str

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.

typeerror can only concatenate str not list to str

Here is an example of how the error occurs.

main.py
my_str = 'fruits: ' my_list = ['apple', 'banana', 'kiwi'] # โ›”๏ธ TypeError: can only concatenate str (not "list") to str print(my_str + my_list)
We tried to use the addition (+) operator to concatenate a string and a list which caused the error.

The values on the left and right-hand sides need to be of compatible types.

# Printing the contents of the list

If you only need to print the contents of the list, use a comma between the string and the list.

main.py
my_str = 'fruits: ' my_list = ['apple', 'banana', 'kiwi'] print(my_str, my_list) # ๐Ÿ‘‰๏ธ fruits: ['apple', 'banana', 'kiwi']

# Concatenate the string to a specific list item

If you meant to concatenate the string and a specific item of the list, access the list at the specific index.

main.py
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.

main.py
my_str = 'number: ' my_list = [1, 2, 3] print(my_str + str(my_list[0])) # ๐Ÿ‘‰๏ธ number: 1

# Adding an item to a list

If you need to add an item to a list, use the append() method.

main.py
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.

# When working with two-dimensional lists

The error is often caused when you have a two-dimensional list.

main.py
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.

main.py
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.

main.py
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.

main.py
my_str = 'fruits:' my_list = ['apple', 'banana'] result = f'{my_str} {my_list}' print(result) # ๐Ÿ‘‰๏ธ fruits: ['apple', 'banana']
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

# Table of Contents

  1. Can only concatenate str (not "NoneType") to str
  2. Can only concatenate str (not "BYTES") to str
  3. Can only concatenate str (not "FLOAT") to str
  4. Can only concatenate str (not "DICT") to str
  5. Can only concatenate str (not "NUMPY.FLOAT64") to str
  6. Can only concatenate str (not "SET") to str
  7. Can only concatenate str (not "NUMPY.INT64") to str

# TypeError: can only concatenate str (not "NoneType") to str

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.

typeerror can only concatenate str not nonetype to str

Here is an example of how the error occurs.

main.py
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.

To solve the error, we either have to figure out where the variable got assigned a None value and correct the assignment or check if the variable doesn't store None before concatenating.

# Common sources of None values in Python

The most common sources of a None value are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

# Functions that lack a return statement return None

Here is an example of getting a None value from a function that doesn't return anything (implicitly returns None).

main.py
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.

main.py
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.

main.py
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.

# Using a fallback value if the variable stores None

Alternatively, you can assign a fallback value to the variable if it is None.

main.py
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.

main.py
name = None result = f'hello {name}' print(result) # ๐Ÿ‘‰๏ธ 'hello None'
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with 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.

# Functions that only return a value if a condition is met

Another common source of None values is having a function that only returns a value if a certain condition is met.

main.py
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.

In all other cases, the function doesn't return anything and ends up implicitly returning 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.

main.py
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.

# Table of Contents

  1. Can only concatenate str (not "BYTES") to str
  2. Can only concatenate str (not "FLOAT") to str
  3. Can only concatenate str (not "DICT") to str
  4. Can only concatenate str (not "NUMPY.FLOAT64") to str
  5. Can only concatenate str (not "SET") to str
  6. Can only concatenate str (not "NUMPY.INT64") to str

# TypeError: can only concatenate str (not "bytes") to str

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.

typeerror can only concatenate str not bytes to str

Here is an example of how the error occurs.

main.py
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
We tried to use the addition (+) operator to concatenate a string and a bytes object which caused the error.

The values on the left and right-hand sides need to be of compatible types.

# Convert the bytes object to a string

One way to solve the error is to convert the bytes object to a string.

main.py
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.

# Convert the string to a bytes object

Alternatively, you can encode the string to a bytes object.

main.py
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.

Either way, you have to make sure that the values on the left and right-hand sides of the addition (+) operator are of compatible types (e.g. two strings).

Once you convert the bytes object to a string, you can use formatted string literals.

main.py
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"
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with 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.

main.py
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.

# Table of Contents

  1. Can only concatenate str (not "FLOAT") to str
  2. Can only concatenate str (not "DICT") to str
  3. Can only concatenate str (not "NUMPY.FLOAT64") to str
  4. Can only concatenate str (not "SET") to str
  5. Can only concatenate str (not "NUMPY.INT64") to str

# TypeError: can only concatenate str (not "float") to str

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).

typeerror can only concatenate str not float to str

Here is an example of how the error occurs.

main.py
# โ›”๏ธ TypeError: can only concatenate str (not "float") to str result = 'it costs: ' + 59.49
We tried to use the addition (+) operator to concatenate a string and a float which caused the error.

The values on the left and right-hand sides need to be of compatible types.

# Convert the floating-point number to a string

One way to solve the error is to convert the float to a string.

main.py
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.

# Convert the string to a floating-point number

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.

main.py
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.

IMPORTANT: if you use the 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.

main.py
str_1 = 'it costs: ' num_1 = 59.49 result = f'{str_1} {num_1} usd' print(result) # ๐Ÿ‘‰๏ธ 'it costs: 59.49 usd'
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with 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.

main.py
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.

main.py
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)

# Convert the input string to a float

The input() function converts the data to a string and returns it.

Even if the user enters a number, it gets converted to a string before it is returned from input().

If you need to convert the result to a number, pass it to the float() (or int()) class.

main.py
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.

# Table of Contents

  1. Can only concatenate str (not "DICT") to str
  2. Can only concatenate str (not "NUMPY.FLOAT64") to str
  3. Can only concatenate str (not "SET") to str
  4. Can only concatenate str (not "NUMPY.INT64") to str

# TypeError: can only concatenate str (not "dict") to str

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.

typeerror can only concatenate str not dict to str

Here is an example of how the error occurs.

main.py
my_str = 'employee: ' my_dict = {'name': 'Bobby Hadz', 'age': 30} # โ›”๏ธ TypeError: can only concatenate str (not "dict") to str print(my_str + my_dict)
We tried to use the addition (+) operator to concatenate a string and a dictionary which caused the error.

The values on the left and right-hand sides need to be of compatible types.

# Printing the contents of a dictionary

If you only need to print the contents of the dictionary, use a comma between the string and the dictionary.

main.py
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.

main.py
my_str = 'employee:' my_dict = {'name': 'Bobby Hadz', 'age': 30} result = f'{my_str} {my_dict}' # ๐Ÿ‘‡๏ธ employee: {'name': 'Bobby Hadz', 'age': 30} print(result)
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

# Concatenate the string and a value of the dictionary

If you meant to concatenate the string and the value of a specific key in the dict, access the key using square brackets.

main.py
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.

main.py
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.

main.py
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.

main.py
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.

# Table of Contents

  1. Can only concatenate str (not "NUMPY.FLOAT64") to str
  2. Can only concatenate str (not "SET") to str
  3. Can only concatenate str (not "NUMPY.INT64") to str

# Can only concatenate str (not "numpy.float64") to str

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.

typeerror can only concatenate str not numpy float64 to str

Here is an example of how the error occurs.

main.py
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
We tried to use the addition (+) operator to concatenate a string and a NumPy float which caused the error.

The values on the left and right-hand sides need to be of compatible types.

# Convert the numpy float to a string

One way to solve the error is to convert the NumPy float to a string.

main.py
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.

# Convert the string to a float to add the two numbers

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.

main.py
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.

IMPORTANT: if you use the 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.

main.py
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'
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with 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.

main.py
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.

# Table of Contents

  1. Can only concatenate str (not "SET") to str
  2. Can only concatenate str (not "NUMPY.INT64") to str

# TypeError: can only concatenate str (not "set") to str

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.

typeerror can only concatenate str not set to str

Here is an example of how the error occurs.

main.py
my_str = 'fruits: ' my_set = {'apple', 'banana', 'kiwi'} # โ›”๏ธ TypeError: can only concatenate str (not "set") to str print(my_str + my_set)
We tried to use the addition (+) operator to concatenate a string and a set which caused the error.

The values on the left and right-hand sides need to be of compatible types.

# Print the contents of a set

If you only need to print the contents of the set, use a comma between the string and the set.

main.py
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.

main.py
my_str = 'fruits:' my_set = {'apple', 'banana', 'kiwi'} result = f'{my_str} {my_set}' print(result) # ๐Ÿ‘‰๏ธ fruits: {'kiwi', 'apple', 'banana'}
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

# Add an element to a set

If you need to add an item to the set, use the add() method.

main.py
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.

main.py
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.

main.py
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.

# Can only concatenate str (not "numpy.int64") to str

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.

typeerror can only concatenate str not numpy int64 to str

Here is an example of how the error occurs.

main.py
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
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.

# Convert the numpy integer to a string

One way to solve the error is to convert the NumPy int to a string.

main.py
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.

# Convert the string to an integer to add the values

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.

main.py
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.

IMPORTANT: if you use the 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.

main.py
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'
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with 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.

main.py
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev