The most flexible and efficient way to convert a list of strings to a string in Python is to use the join() method, which joins the elements of a list into a single string with a separator.
However, if the list contains both strings and integers, the method requires converting the non-string elements to strings first, as it cannot directly concatenate different data types.
new_list = ['I', 'Love', 'Python'] print(new_list) # Output: ['I', 'Love', 'Python'] print(type(new_list)) # Output: <class 'list'> new_string = ' '.join(new_list) # Joins elements with a space print(new_string) # Output: 'I Love Python' print(type(new_string)) # Output: <class 'str'>
We used the type() function to print the variable’s type before and after conversion to verify it. You can see that the type is changed from “list” to “str”.
Custom separator (e.g., newline for multiline output)
You can pass the custom separator based on your requirement. For example, in a basic use case, we pass an empty space (” “), but let’s pass the next line character “\n”, and after each element, it will print the newline character, and hence we will get a string vertically.
line_list = ['line1', 'line2', 'line3'] print(line_list) # Output: ['line1', 'line2', 'line3'] print(type(line_list)) # Output: <class 'list'> vertical_string = '\n'.join(line_list) print(vertical_string) # Output: # line1 # line2 # line3 print(type(vertical_string)) # Output: <class 'str'>
List of non-strings
What if the list contains integers or floats instead of strings? In that case, we have to convert the elements into a string and then combine them into a single string.
integer_list = [11, 21, 46] print(integer_list) # Output: [11, 21, 46] print(type(integer_list)) # Output: <class 'list'> num_string = ', '.join(map(str, integer_list)) print(num_string) # Output: 11, 21, 46 print(type(num_string)) # Output: <class 'str'>
Empty list (No error, empty string)
If the list is empty, the output string will also be empty, as there are no items in the list.
empty_list = [] print(empty_list) # Output: [] print(type(empty_list)) # Output: <class 'list'> empty_string = ', '.join(empty_list) print(empty_string) # Output: "" (Empty string) print(type(empty_string)) # Output: <class 'str'>
Alternate approaches
Using str(): simple representation (including brackets and quotes)
For quick debugging and inspection, you can use the built-in str() function, which returns a string representation of a list, including quotes and brackets in the output. So, the output string won’t be clean.
new_list = ['I', 'Love', 'Python'] print(new_list) # Output: ['I', 'Love', 'Python'] print(type(new_list)) # Output: <class 'list'> string_with_brackets = str(new_list) print(string_with_brackets) # Output: "['I', 'Love', 'Python']" print(type(string_with_brackets)) # Output: <class 'str'>
To remove brackets and quotes, use the combination of slicing and the str.replace() method.
new_list = ['I', 'Love', 'Python']
print(new_list)
# Output: ['I', 'Love', 'Python']
print(type(new_list))
# Output: <class 'list'>
result = str(new_list)[1:-1] # Removes brackets
clean_string = result.replace("'", "") # Further cleanup if needed
print(clean_string)
# Output: I, Love, Python
print(type(clean_string))
# Output: <class 'str'>
Using map()
When the list contains elements of mixed data types (str, int, and bool), always convert all the elements to a one type using the map() method.
The map(str, list) method will help us convert each element of the list to a string, and then we can join them by a custom separator. It keeps memory efficient.
new_list = ['I', 5, 'Love', 10, 'Python'] print(new_list) # Output: ['I', 5, 'Love', 10, 'Python'] print(type(new_list)) # Output: <class 'list'> new_string = ' '.join(map(str, new_list)) print(new_string) # Output: "I 5 Love 10 Python" print(type(new_string)) # Output: <class 'str'>
Using a for loop (Manual conversion)
A for loop iterates over each element in the list, concatenating each element to a string, and adds a space after each one for separation.
new_list = ['I', 5, 'Love', 10, 'Python']
print(new_list)
# Output: ['I', 5, 'Love', 10, 'Python']
print(type(new_list))
# Output: <class 'list'>
new_string = ''
for item in new_list:
# Converting item to string and adding a space
new_string += str(item) + ' '
new_string = new_string.strip() # Removing the trailing space
print(new_string)
# Output: I 5 Love 10 Python
print(type(new_string))
# Output: <class 'str'>
That’s all!



fortuit
hello, do you know how to get this result ?
“Millie”,”Bobby”,Brown”,”is”,”Enola”,”Holmes”,1,2,3
The idea is to still see the quotes in the output ?