What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

4 Ways to Convert List to String in Python (with Code)

  • Apr 26, 2021
  • 6 Minute Read
4 Ways to Convert List to String in Python (with Code)

There are many situations in which we need to convert the python list into strings. In this article, we will what are lists in python and different methods to convert the list into the string in python. So, let's get started.

Lists in Python

The list is the most important data type in python language. In Python language, the list is written as the list of commas separated values inside the square bracket. The most important advantage of the list is the elements inside the list are not compulsorily be of the same data type along with negative indexing. Also, all the operation of the string is similarly applied on list data type such as slicing, concatenation, etc. Also, we can create the nested list i.e list containing another list.

For example:

# creating a list of items with different data types 
sam_list = [8,"tom",[M,N]] 
print(sam_list)

Output:

 [8, 'tom', ['M', 'N']]

What are Strings?

A string is generally a sequence of characters. A character is a simple symbol. For example, in the English Language, we have 26 characters available. The computer system does not understand characters and hence, therefore, deal with binary numbers. Even though we can see characters on our monitor screens, but internally it is stored and manipulated as a combination of 0s and 1s. The conversion of characters and the binary number is called encoding, and the reverse of this is known as decoding.  Some of the popular encodings are ASCII and Unicode. In the Python programming language, a string is a sequence of Unicode characters.

How to convert a list to a string python?

There are many methods to convert a list to a string in python programming. Let us study one by one in detail.

1) Iterating through the list

In this method, we will add the element for every index in some empty string while iterating the list.

Code:

# Python program to convert a list to string 
# # Function to convert 
def listToString(s): 
    
    # initialize an empty string 
    string = "" 
    
    # traverse in the string 
    for an element in s: 
        string += element 
    
    # return string 
    return string 
        
        
# Driver code    
s = ['Apple ', 'is ', 'Red'] 
print(listToString(s))

Output:

 Apple is Red

2) Using .join() method

In this method, we use the join() method. But this method is not going to work if the list contains both string and integer as its element.

Code:

# Python program to convert a list 
# to string using join() function 
    
# Function to convert 
def listToString(s): 
    
    # initialize an empty string 
    string = " "
    
    # return string 
    return (string.join(s)) 
        
        
# Driver code    
s = ['Apple ', 'is ', 'Red'] 
print(listToString(s)) 

Output:

 Apple is Red

3) Using List Comprehension

When we have a list that contains integers and string both at the same time then we use this method. Here we convert the list into a string while adding it to a string.

Code:

# Python program to convert a list 
# to string using list comprehension 

s = ['I', 'ate', 2, 'shake', 'and', 3, 'chocolates'] 

# using list comprehension 
listToStr = ' '.join([str(element) for element in s]) 

print(listToStr) 

Output:

 I ate 2 shake and 3 chocolates

4) Using map() function

Using map() method for mapping string from the list with a given iterator.

Code:

# Python program to convert a list 
# to string using list comprehension 

s = ['I', 'ate', 2, 'shake', 'and', 3, 'chocolates'] 

# using list comprehension 
listToStr = ' '.join(map(str, s)) 

print(listToStr) 

Output:

 I ate 2 shake and 3 chocolates

Conclusion

Here in the above article, we studied what is a list and what is string in the python language. Further, we learned the different methods to convert the list to string in python. Also, we went through the example code and its output along with it.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Shivali Bhadaniya
I'm Shivali Bhadaniya, a computer engineer student and technical content writer, very enthusiastic to learn and explore new technologies and looking towards great opportunities. It is amazing for me to share my knowledge through my content to help curious minds.