How to Convert Python List to String (4 Ways)
In this article, we will see various ways to “Convert Python List To String“. It will help you in implementing this concept and get a tight grip over this.
There are various situations we might encounter when a list is given and we convert it to a string. For example, conversion to string from the list of string or the list of integer.
In general, we face many situations where we have to convert a list to a string. Here we take a list of string and convert them to string.
1 2 |
Input: ['Software', 'Testing', 'Material'] Output: Software Testing Material |
1 2 |
Input: ['I', 'want', 'to', 'learn', 'Software', 'Testing', 'in', 2020] Output: I want to learn Software Testing in 2020 |
Here I will showcase different ways to convert the list to String in Python.
Method 1: Using Join Method:
We can use the join method to convert it to a string
Input using a space ” “:
1 |
mylist =['Software', 'Testing', 'Material']<br />print(" ".join(mylist)) |
Output:
1 |
Software Testing Material |
Input using a comma “,”:
1 |
mylist =['Software', 'Testing', 'Material']<br />print(",".join(mylist)) |
Output:
1 |
Software,Testing,Material |
Input using a comma “n”:
1 |
mylist =['Software', 'Testing', 'Material']<br />print("n".join(mylist)) |
Output:
1 |
Software<br />Testing<br />Material |
Here is the sample program to convert string to list using Join method
1 2 3 |
# Write a program in Python to convert a list to string using join() function # Function to convert <br />def listToString(myList): <br /><br />return myList<br /><br /># List of Strings <br />mylist = ['Software', 'Testing', 'Material']<br /><br /># List of Strings separated with a space<br />print("Seperated with space: ", " ".join(mylist))<br /><br /># List of Strings separated with a comma<br />print("Seperated with comma: ", ",".join(mylist))<br /><br /># List of Strings separated with new line<br />print("Seperated with new line: ")<br />print("\n".join(mylist)) |
Output:
1 |
Seperated with space: Software Testing Material<br />Seperated with comma: Software,Testing,Material<br />Seperated with comma: <br />Software<br />Testing<br />Material |
However, this simple method does not work if the list contains non-string objects, such as integers.
Method 2: Using Join Method When There are Integers
1 |
# List of Strings<br />mylist = ['I', 'want', 'to', 'learn', 'Software', 'Testing', 'in', 2020]<br /><br /># Let's convert sourceList to a list of strings and then join its elements.<br />listToStr = ' '.join([str(ele) for ele in mylist ])<br /><br />print(listToStr) |
Output:
I want to learn Software Testing in 2020
Method 3: Iterating through the list of strings and adding them for every index in an empty string.
1 |
# Write a program in python to convert a list to string <br /><br /># Function to convert <br />def listToString(s): <br /><br /># initializing an empty string <br />str1 = " " <br /><br /># traverse in the string <br />for ele in myList: str1 += ele <br /><br /># return string <br />return str1 <br /><br />myList = ['Software', 'Testing', 'Material'] <br />print(listToString(myList)) |
Output:
1 |
SoftwareTestingMaterial |
Also read: