Python – Convert a list of characters into a string



Python | Convert a list of characters into a string

Given a list of characters, merge all of them into a string.

Examples:

Input : ['g', 'e', 'e', 'k', 's', 'f', 'o', 
             'r', 'g', 'e', 'e', 'k', 's']
Output : geeksforgeeks 

Input : ['p', 'r', 'o', 'g', 'r', 'a', 'm', 
                        'm', 'i', 'n', 'g']
Output : programming 

Method 1 : Traversal of list

# Python program to convert a list
# of character
 
def convert(s):
 
    # initialization of string to ""
    new = ""
 
    # traverse in the string 
    for x in s:
        new +=
 
    # return string 
    return new
     
     
# driver code   
s = ['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
print(convert(s))

Output:

geeksforgeeks

Method 2 : Using join() function

By using join() function in python, all characters in the list can be joined. The syntax is:

str = ""
str1 = ( "Python", "for", "pandas" )
str.join(str1) 

The list of characters can be joined easily by initializing str=”” so that there are no spaces in between.

# Python program to convert a list
# of character
 
def convert(s):
 
    # initialization of string to ""
    str1 = ""
 
    # using join function join the list s by 
    # separating words by str1
    return(str1.join(s))
     
# driver code   
s = ['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
print(convert(s))

Output:

geeksforgeeks

Last Updated on October 3, 2021 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs