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.
Input: ['Software', 'Testing', 'Material'] Output: Software Testing Material
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 ” “:
mylist =['Software', 'Testing', 'Material']
print(" ".join(mylist))
Output:
Software Testing Material
Input using a comma “,”:
mylist =['Software', 'Testing', 'Material']
print(",".join(mylist))
Output:
Software,Testing,Material
Input using a comma “n”:
mylist =['Software', 'Testing', 'Material']
print("n".join(mylist))
Output:
Software
Testing
Material
Here is the sample program to convert string to list using Join method
# Write a program in Python to convert a list to string using join() function # Function to convert
def listToString(myList):
return myList
# List of Strings
mylist = ['Software', 'Testing', 'Material']
# List of Strings separated with a space
print("Seperated with space: ", " ".join(mylist))
# List of Strings separated with a comma
print("Seperated with comma: ", ",".join(mylist))
# List of Strings separated with new line
print("Seperated with new line: ")
print("\n".join(mylist))
Output:
Seperated with space: Software Testing Material
Seperated with comma: Software,Testing,Material
Seperated with comma:
Software
Testing
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
# List of Strings
mylist = ['I', 'want', 'to', 'learn', 'Software', 'Testing', 'in', 2020]
# Let's convert sourceList to a list of strings and then join its elements.
listToStr = ' '.join([str(ele) for ele in mylist ])
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.
# Write a program in python to convert a list to string
# Function to convert
def listToString(s):
# initializing an empty string
str1 = " "
# traverse in the string
for ele in myList: str1 += ele
# return string
return str1
myList = ['Software', 'Testing', 'Material']
print(listToString(myList))
Output:
SoftwareTestingMaterial
Also read:
- Python Strings
- Python Data Types
- Python Multiline String
- Python Join() Method with Examples
- Python float() Function Explained with Examples
- Python Interview Questions