Python program to convert a list to string

Given a list, write a Python program to covert the given list to string.

There are various situation we might encounter when a list is given and we convert it to string. For example, conversion to string from the list of string or the list of integer.

Example:



Input: ['Geeks', 'for', 'Geeks']
Output: Geeks for Geeks

Input: ['I', 'want', 4, 'apples', 'and', 18, 'bananas']
Output: I want 4 apples and 18 bananas

Let’s see various ways we can convert the list to string.

Method #1:
Iterate through the list and keep adding the element for every index in some empty string.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

GeeksforGeeks

Method #2: Using .join() method



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Geeks for Geeks

But what if the list contains both string and integer as its element. In those cases, above code won’t work. We need to convert it to string while adding to string.

Method #3: Using list comprehension

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to convert a list
# to string using list comprehension
   
s = ['I', 'want', 4, 'apples', 'and', 18, 'bananas']
  
# using list comprehension
listToStr = ' '.join([str(elem) for elem in s])
  
print(listToStr) 

chevron_right


Output:

I want 4 apples and 18 bananas

Method #4: Using map()
Use map() method for mapping str (for converting elements in list to string) with given iterator, the list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to convert a list
# to string using list comprehension
   
s = ['I', 'want', 4, 'apples', 'and', 18, 'bananas']
  
# using list comprehension
listToStr = ' '.join(map(str, s))
  
print(listToStr) 

chevron_right


Output:

I want 4 apples and 18 bananas


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.