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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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': 'Alice', '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.

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': 'Alice', 'age': 30} # 👇️ employee: {'name': 'Alice', 'age': 30} print(my_str, my_dict)

Alternatively, you can use a formatted string literal.

main.py
my_str = 'employee:' my_dict = {'name': 'Alice', 'age': 30} result = f'{my_str} {my_dict}' # 👇️ employee: {'name': 'Alice', '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}.

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': 'Alice', 'age': 30} result = my_str + my_dict['name'] # 👇️ employee: Alice 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.

You can also convert the dict to a string by passing it to the str() class.

main.py
my_str = 'employee: ' my_dict = {'name': 'Alice', 'age': 30} result = my_str + str(my_dict) # 👇️ employee: {'name': 'Alice', '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.

Conclusion #

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.

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.