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
Initialize an empty string at the beginning. Traverse in the list of characters, for every index add character to the initial string. After complete traversal, print the string which has been added with every character.
# 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 += x # 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 = ( "geeks", "for", "geeks" ) 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
Recommended Posts:
- Python | Convert list of strings and characters to list of characters
- Python | Convert list of numerical string to list of Integers
- Python | Convert list of string into sorted list of integer
- Python | Convert a string representation of list into list
- Python | Convert string enclosed list to list
- Python | Convert list of string to list of list
- Python | Splitting string to list of characters
- Python | Splitting string to list of characters
- Python | Split string into list of characters
- Python program to convert a list to string
- Python | Program to convert String to a List
- Python | Ways to convert list of ASCII value to string
- Python | Convert list of strings to space separated string
- Convert given string so that it holds only distinct characters
- Convert characters of a string to opposite case
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.
Improved By : nidhi_biet



