In this tutorial, you can quickly discover the most efficient methods to convert Python List to String. Several examples are provided to help for clear understanding.
Python provides a magical join[] method that takes a sequence and converts it to a string. The list can contain any of the following object types: Strings, Characters, Numbers.
While programming, you may face many scenarios where list to string conversion is required. Lets now see how can we use the join[] method in such different cases.
How to Convert Python List to String
As weve told above that Python Join[] function can easily convert a list to string so lets first check out its syntax.
Python Join[] Syntax
The syntax of join[] is as follows:
The above method joins all the elements present in the iterable separated by the string_token. Next, you will see the join[] function examples to convert list to a string.
Convert Python List of Strings to a string using join[]
Lets say we have a list of month names.
# List of month names listOfmonths = ["Jan" , "Feb", "Mar", "April", "May", "Jun", "Jul"]Now, well join all the strings in the above list considering space as a separator.
""" Desc: Function to convert List of strings to a string with a separator """ def converttostr[input_seq, seperator]: # Join all the strings in list final_str = seperator.join[input_seq] return final_str # List of month names listOfmonths = ["Jan" , "Feb", "Mar", "April", "May", "Jun", "Jul"] # List of month names separated with a space seperator = ' ' print["Scenario#1: ", converttostr[listOfmonths, seperator]] # List of month names separated with a comma seperator = ', ' print["Scenario#2: ", converttostr[listOfmonths, seperator]]The output is as follows:
Scenario#1: Jan Feb Mar April May Jun Jul Scenario#2: Jan, Feb, Mar, April, May, Jun, JulConvert a list of chars into a string
With the help of join[] method, we can also convert a list of characters to a string. See the example given below:
The output is as follows:
['p', 'y', 't', 'h', 'o', 'n', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g'] python programmingConversion of a mixed list to a string using join[]
Lets assume that we have got a list of some numbers and strings. Since it is a combination of different objects, so we need to handle it a little differently.
sourceList = ["I" , "got", 60, "in", "Science", 70, "in", "English",", and", 66, "in", "Maths"]It is not possible to use the join[] function on this list. We first have to convert each element to a string to form a new one, and then only we can call join[] method.
# Let's convert sourceList to a list of strings and then join its elements. stringList = ' '.join[[str[item] for item in sourceList ]]The final string would appear something like:
I got 60 in Science, 70 in English, and 66 in Maths.You can check out the full working code below:
Get a comma-separated string from a list of numbers
If you simply wish to convert a comma-separated string, then try the below shortcut:
numList = [20, 213, 4587, 7869] print[str[numList].strip['[]']]The output is as follows:
20, 213, 4587, 7869Alternatively, we can use map[] function to convert the items in the list to a string. After that, we can join them as below:
print [', '.join[map[str, numList]]]The output:
20, 213, 4587, 7869We can even use the new line character [\n] as a separator. See below:
print ['\n'.join[map[str, numList]]]The result is as follows:
20 213 4587 7869Must Read Python Replace String with Examples