If you are getting and don’t know how to fix the TypeError: can only concatenate str (not “list”) to str, let’s read this article now. It will give you the easiest way to fix this error.
When does the “TypeError: can only concatenate str (not “list”) to str” happen?
This error occurs when you can try to concatenate the string to the list in Python that is unsupported.
See the code below to understand this problem.
Code example:
# Initialize a string str = "Learn" # Initialize a list list = ["Share", "IT"] # Concatenate string and list together print(str + list)
Output:
TypeError: can only concatenate str (not "list") to str
So we know the cause of this error, here we will go to find out the solutions to fix this error.
How to fix this error?
Replacing ‘ + ’
to ‘ , ’
Here, we can fix this error by replacing '+'
with ','
. Now we will print a string and a list to the screen without concatenating the string and the list. Follow the code example:
# Initialize a string str = "Learn" # Initialize a list list = ["Share", "IT"] # Concatenate string and list together print(str, list)
Output:
Learn ['Share', 'IT']
Accessing the index
We can concatenate a string and a list of ways to access the index in the list, and the object at the index in the list must be a string because a string plus a string won’t cause an error.
So, first of all, we will go through all the elements in the list. Second, we create variable str, then update variable str by adding an object at the index in a list.
Code example:
# Initialize a string str = "Learn" # Initialize a list list = ["Share", "IT"] # Use a for loop to iterate through the elements in the list for l in list: str = str + " " + l # Update str print("After concatenate:",str)
Output:
After concatenate: Learn Share IT
Using append() method
You can use the append() method to add a string to a list. This will not cause an error.
Syntax:
list.append(str)
Parameters:
str:
a string you want to append.
Code example:
# Initialize a string str = "Learn" # Initialize a list list = ["Share", "IT"] # Append str into a list list.append(str) # Method 1: print list print("List after append:",list) # Method 2: print items in list for i in list: print(i,end= " ")
Output:
List after append: ['Share', 'IT', 'Learn']
Share IT Learn
Summary
Above are all the simplest solutions I suggest for you to fix the “TypeError: can only concatenate str (not “list”) to str”. Hope you find the best solution for you. If you have any questions, please leave a comment below. I will answer as possible. Thanks for reading!
Maybe you are interested:
- TypeError: float() argument must be a string or a real number, not ‘list’
- TypeError: ‘tuple’ object does not support item assignment
- TypeError: ‘bool’ object is not callable in Python
Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript