How to Convert List to String in Python | The List is the container that contains the elements of different datatypes whereas a String is a sequence of characters it contains only similar elements. Python provides functionality to convert a list to a string. Also see:- How to Compare Two Lists in Python
We will see these below Python program examples:-
- Using Join Function
- Using map() Function
- Using List Comprehension
- Traversal of a List Function
- Python list to string with commas
- Python list to string with delimiter
How to Convert List to String in Python
Now, we will see various ways to convert a list to a string in Python.
Python List to String using join() function
We will convert the list to string by using the join function. This function directly converts the list to a string. To do achieve this we have defined a function called “Main” which uses join.
def Main(s): string = " " return (string.join(s)) s = ['Know', 'Program'] print(Main(s))
Python List to String using map() function
The map() is an iterable function, which returns the map object. Now we use map() to convert list to string.
list = ['Know' 'Program',1] string = ' '.join(map(str, list)) print(string)
Python List to String using List Comprehension
The list comprehension is used for creating a new list from iterables like string, tuple, and more. It provides a single line syntax.
list = ['know', 'program'] Str = ' '.join([str(elem) for elem in list]) print(Str)
Python List to String Traversal of a List Function
This keeps on iterating through the list adds keeps adding elements to the empty string.
def listToString(s): str1 = "" for ele in s: str1 += ele return str1 s = ['know', 'program'] print(listToString(s))
Python List to String with Commas
Now, we will convert a list to a string with commas using join function.
list = ["know", "Program"] string = ", ".join(list) print(string)
Python List to String with Delimiter
The Delimeter is one or more characters used to specify a difference between the text. In the program, we have used ‘/’ as a delimiter.
list = ['know', 'program'] delimeter = "/" res = '' for ele in list: res = res + str(ele) + delimeter print(str(res))
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!