The fastest way to convert a list (array) to a string is to use the join function.
my_string = ['This', 'is', 'a', 'string'] print(' '.join(my_string))
The join function concatenates all elements inside the list and uses spaces as separators.
The result of this code is this string.
This is a string
This code is easily modifiable when you want to change a separator to different one. Let’s try a comma.
my_string = ['This', 'is', 'a', 'string'] print(','.join(my_string))
By changing one character, you are getting this result.
Int List to String
If you try to convert a list that contains integer values instead of strings, Python is going to return an error.
TypeError: sequence item 0: expected str instance, int found
We have to convert integer values to strings before we can join them. For this task, we are going to use the for loop.
my_ints = [1, 22, 13, 214] my_string = '' for my_int in my_ints: my_string += ',' + str(my_int) my_string = my_string[1:] print(my_string)
The problem with this approach is that it adds a comma at the beginning of the string which we want to avoid. That’s why we add a line my_string = my_string[1:] that removes the first character and assigns this value to my_string.
This result can be also achieved by using a range inside the loop and then adding a separator after the first iteration (which starts from 0).
my_ints = [1, 22, 13, 214] my_string = '' for i in range(len(my_ints)): if i > 0: my_string += ',' my_string += str(my_ints[i]) print(my_string)
The result is the same.
The quickest and most Pythonic way to do it, is to convert integers to string in one line. Here’s the code.
my_ints = [1, 22, 13, 214] print(','.join(str(x) for x in my_ints))
The result is the same as before but the code uses only two lines.
Another simple way to convert a list of ints to a string is to use the map function. There is no need to use loops in this case.
my_ints = [1, 22, 13, 214] print(','.join(map(str, my_ints)))
In our case, the map function takes a list as a second parameter and applies the str function. In other words, it converts integers inside the list to strings. The join function concatenates these elements, using a comma as a separator. The result is the same as in previous examples.
We can also convert a list of lists to a string. The elements in our list consist of multiple smaller lists. In these lists are both strings and integer values. We have to convert these values to strings and then join them to create a one-dimensional list.
This is how the code looks like:
my_list = [[1, '1'], , ['This', 'is', 3]] result_list = [' '.join([str(c) for c in lst]) for lst in my_list] print(result_list)
This is how the result looks like:
['1 1', '2', 'This is 3']
This list has to be joined to create a single string. We can use the method from the first example. The full code looks like this:
my_list = [[1, '1'], , ['This', 'is', 3]] result_list = [' '.join([str(c) for c in lst]) for lst in my_list] my_string = ' '.join(result_list) print(my_string)
As expected, the result is a string.
1 1 2 This is 3
Convert List to String With Quotes
String values are written inside quotes. If you want to add quotes to a string you can’t just add another quote between quotes, because Python will return the syntax error. If you use three quotes “””text”””, the result will be the same as with single quotes.
SyntaxError: invalid syntax
What you have to do is to use different quotes. You can do it for single quotes.
my_list = ["This", "is", "'text'", "string"] my_string = ' '.join(my_list) print(my_string)
This is 'text' string
You can also do it for double quotes.
my_list = ['This', 'is', '"text"', 'string'] my_string = ' '.join(my_list) print(my_string)
This is "text" string
You can also combine both quotes.
my_list = ['This', 'is', '"text"', "'string'"] my_string = ' '.join(my_list) print(my_string)
This is "text" 'string'