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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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.

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']

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

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

Conclusion #

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.

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.