How to convert list to string in Python?
Before studying further about this topic we should learn about List and String.
What is List?
The python lists are containers that are used to store a list of values of any type. Unlike other variables python lists are mutable i.e., we can change the elements of a list in place; Python will not create a fresh list when you make changes to an element of a list.
list=['A', 'B', 'C', 'D', 'E'] list type(list)
output- ['A', 'B', 'C', 'D', 'E'] list
What is String?
Python strings are characters enclosed in quotes of any type- single quotation marks, double quotation marks and triple quotation marks. Python strings are immutable.
string="Python" string type(string)
output- Python string
Convert list into string
We can use join method to convert list into string.
list1 =["pythonpoint","python","blog"] print(",".join(list1)) list1 =["pythonpoint","python","blog"] print("n".join(list1))
output- pythonpoint python blog
We can use str to convert list into string.
n=[1,5,8,2,7] print(str(n)) print(str(n)[1:-1]) print(str(n).strip(''))
Alternatively, we can use map() function to convert the items in the list to a string. After that, we can join them
n=[1,2,3,4,5] print(str(n)) print (', '.join(map(str, n)))