Given a list, write a Python program to convert 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.
# 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)) |
GeeksforGeeks
Method #2: Using .join() method
# 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)) |
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
# 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) |
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.
# 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) |
I want 4 apples and 18 bananas
Recommended Posts:
- Python | Convert List of String List to String List
- Python | Convert list of string to list of list
- Python Program to convert List of Integer to List of String
- Python | Convert list of tuples to list of list
- Python program to convert a list of strings with a delimiter to a list of tuple
- Python | Convert a string representation of list into list
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Convert string enclosed list to list
- Python | Convert mixed data types tuple list to string list
- Python | Convert string List to Nested Character List
- Python - Convert String List to Key-Value List dictionary
- Python | Program to convert String to a List
- Python | Convert a nested list into a flat list
- Python | Convert list of strings and characters to list of characters
- Python | Convert list of tuples into list
- Python | Convert list of tuples to list of strings
- Python | Convert given list into nested list
- Python | Convert list into list of lists
- Python | Convert list of strings to list of tuples
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


