How to Convert a List to String in Python
In this article, we will learn to convert a list to a string in Python. We will use some built-in functions and some custom code as well. Let's first have a quick look over what is list and string in Python.
Python List
Python has a built-in data type called list. It is like a collection of arrays with different methodology. Data inside the list can be of any type say, integer, string or a float value, or even a list type. The list uses comma-separated values within square brackets to store data. Lists can be defined using any variable name and then assigning different values to the list in a square bracket. The list is ordered, changeable, and allows duplicate values.
list1 = ["Ram", "Arun", "Kiran"]
list2 = [16, 78, 32, 67]
list3 = ["apple", "mango", 16, "cherry", 3.4]
Python String
The String is a type in python language just like integer, float, boolean, etc. Data surrounded by single quotes or double quotes are said to be a string. A string is also known as a sequence of characters.
string1 = "apple"
string2 = "Preeti125"
string3 = "12345"
string4 = "pre@12"
There are various use cases or scenarios where a string is needed but a list enclosed within square brackets is given to the programmer. In the conversion of a list to a string, comma-separated values are printed with a single space or any separator that is defined between individual data.
Convert List to String using join() function
Python provides a built-in join()
method that takes a sequence of characters and converts it to a single string. The list can contain any of the following object values: Strings, Characters, or Numbers. The join method excepts all the elements present in the iterable separated by the string_token.
Syntax
join(iterable)
iterable: It could be a list of strings, characters, and numbers.
Example
In this example, we are using join()
method to get a string from a list. This method returns a string after converting the iterable. See the below example.
def listToString(s):
# initialize an empty string
string1 = " "
return (string1.join(s))
# Driver code
s = ["Apple", "is", "a", "fruit"]
print(listToString(s))
Apple is a fruit
With the help of the join() method, we can also convert a list of characters to a string. See the example given below:
Example
chrlist1 = ['s','t','u','d','y',' ','t','o','n','i','g','h','t']
newString = ''.join(chrlist1)
print(newString)
study tonight
Convert List to String Using For Loop
This method will Iterate through the list and keep adding the elements of the list one by one for every index in some empty string defined.
Example
def convert(s):
str1 = " "
for ele in s:
str1 += ele
return str1
#Driver code
s = ["Apple", "is", "a", "fruit"]
print(convert(s))
Appleisafruit
Convert List to String Using List Comprehension
It creates another list based on the first set of values of an existing list. It converts the mixed list to a string using the join() function. Let’s assume that we've got a list of some numbers and strings. Since it is a combination of various objects, so we need to handle it a bit differently. It is not possible to use the join() function alone on this list. We first should convert each element to a string to create another one, then only we will call the join() method.
Example
s = ['I', 'have', 1, 'younger', 'brother', 'and', 2, 'elder', 'sisters']
listToStr = ' '.join([str(elem) for elem in s])
print(listToStr)
I have 1 younger brother and 2 elder sisters
Convert List to String Using the str() function
The str() is a built-in function of String that can be used to convert any type of data into a string. See the example below.
Example
num_list=[1,56,87,22.3,76]
#Now manipulate the str() to get string of just numbers
print(str(num_list))
1,56,87,22.3,76
Conclusion
In this article, we learned to convert a list to a string by using several built-in functions such as str()
, join()
, etc and we used some custom code as well. For example, we used for-loop to iterate the elements of the list and then add them into the string to get a string at the end.