Sometimes the programmer is faced with the task of converting an iterated object (most often a list) into a string. Typically, this is done for display somewhere: printing to a console or writing to a file. Another unobvious reason to convert a list into a string is to use string methods and functions that work with this data type. For example, searching for complex conditions is sometimes easier to implement through regular expressions. In Python, if you do such a conversion “head-on”, it is not pretty:
var = [1, '2', 3] print(var) print(str(var)) # Output: [1, '2', 3] [1, '2', 3]
You will learn how to do it nicely in this lesson.
Converting a list into a string using the join method
The most common method used to convert a list into a string is the python string join() method. This method takes an iterable object as an argument. Recall that iterable objects are those that can be searched. For example, a list, a set, a tuple, and by the way, a string. The object (string) to which the method is applied will be the separator. Here’s how it works:
var = ['1', '2', '3'] print(', '.join(var)) # Output: 1, 2, 3
As you can see, the string ‘, ‘ has been inserted between the elements of the var list and thus a new string is formed, already without square brackets. There is one serious limitation: all elements that are combined must be of type string. If this condition is not met, the interpreter will return an exception:
var = [1, 2, 3] print(', '.join(var)) # Output: Traceback (most recent call last): File "C:\Users\Dushenko\AppData\Roaming\JetBrains\PyCharm2021.1\scratches\scratch.py", line 3, in print(', '.join(var)) TypeError: sequence item 0: expected str instance, int found Process finished with exit code 1
Why join() is a string method and not a list method?
The syntax of the method seems a bit awkward. Why not make it like this:
var = [1, 2, 3] print(var.join(', ')) # Output: But no.. Traceback (most recent call last): File "C:\Users\Dushenko\AppData\Roaming\JetBrains\PyCharm2021.1\scratches\scratch.py", line 3, in print(var.join(', ')) AttributeError: 'list' object has no attribute 'join' Process finished with exit code 1
Many people have a question: why is the join() method implemented this way in Python? Why does it belong to the string type? The whole point is that this method can work with any iterated object, which means that if the method belonged to an iterated object, it would have to be implemented for every iterated type. On the other hand, the separator is always a string and the current syntax only allows the Python join() method to be implemented for one type – string.
Splitting a string with join()
I mentioned at the beginning that strings can also be iterated. This means that the method can be applied to strings, too. The elements of a sequence in this case are symbols. Here is an example:
var = '123' print(', '.join(var)) # output: 1, 2, 3
Inverse conversion of a string to a list
If you need to perform the inverse operation of creating a list from a string, you can use the split() string method, which is very similar to join(). Here’s a simple example:
var = '1, 2, 3'.split(', ') print(var) print('Type:', type(var)) # Output: ['1', '2', '3'] Type:
Notice that here the object-argument order is reversed. This means that the string to be split is on the left, before the dot, and the separator is passed as an argument.
Combining a list with non string data types
As mentioned earlier, all list elements must be strings. If the join() method must be used and the condition is not met, a type conversion must be performed.
Using join() and map()
The map() function takes two positional arguments: the first is the function to be applied to each element of the sequence; the second is the sequence to which the function is applied. The function returns a special iterator object, so this object must be converted into a list to get results. An example of using the map() function:
var = [1, 2, 3] var = map(print, var) print('var:', var) var = list(var) print('var:', var) # Output: var: 1 2 3 var: [None, None, None]
As you can see, map() returned an iterator map object, and in converting to a list, it applied print() to each item in the original list. Let’s return to join(). We were talking about a case where you want to convert nonstring elements to string elements. Here’s how to do it with map():
var = [1, 2, 3] var = map(str, var) print(', '.join(var)) # Output: 1, 2, 3
Using a loop
An alternative is to use a loop:
var = [1, 2, 3] new_var =  for item in var: new_var.append(str(item)) print(', '.join(new_var)) # Output: 1, 2, 3
This option is good if you are only familiar with basic Python constructs.
Using list inclusion
Another option for converting the type of sequence elements:
var = [1, 2, 3] print(', '.join([str(item) for item in var])) # Output: 1, 2, 3
I personally find the map() variant to be the best. At least, it works the fastest. The list inclusion is good for working in the console.
Alternatives to the join() method
Python provides other ways to achieve the same result as the join() method.
Concatenation is the concatenation of strings. It is performed with a “+” character. You can use a low-level (imperative) approach and write your own algorithm that imitates join(). To do that, we use concatenation:
var = [1, 2, 3] str_var = '' for item in var: str_var += str(item) + ', ' str_var = str_var[:-2] print(str_var) # Output: 1, 2, 3
Of course, this is verbose and much slower, so I recommend it as an exercise only.
The strip() method
Let’s go back to the beginning and remember what a list looks like when converted to a string:
var = [1, 2, 3] print(var) # Output: [1, 2, 3]
If you want comma-separated strings, you can use the strip() method and remove the square brackets:
var = [1, 2, 3] print(str(var).strip('')) # Output: 1, 2, 3
But, if there will be strings or other types other than numeric in the list, you will have to do extra actions to clear the string. You can say that this is bad, inflexible code. Just know that there is such a possibility.
var = [1, '2', complex(3)] print(str(var).strip('')) # Output: 1, '2', (3+0j)