Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (50.2k points)

Can anyone tell me how to convert the list to string in Python?

1 Answer

0 votes
by (108k points)

There are many methods of converting a list into a string in Python, refer to the below methods:

In the first method, you have to create a function, and inside the function, pass the list. Then, you can just add all the elements of the list in an empty string:

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 = ['Python ', 'is ', 'a ', 'programming ','language ' ]

print(listToString(s))  

The second way is to use the join() function as shown below:

def listToString(s):

# initialize an empty string

    str1 = " "

# return string  

    return (str1.join(s))

# Driver code

s = ['Python', 'is', 'a', 'programming','language' ]

print(listToString(s))

If you are looking for an online course to learn Python, check out this Python Course by Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 13, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
0 votes
1 answer
asked Aug 26, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...