How to convert python list to string
2 weeks ago codedestine 0
In this tutorial, we will learn about converting python list object to string.
We can use String’s join method for converting List object to String.
Syntax : < String Object >.join(< Iterable Object >)
This method returns a string, which is a concatenation of all the strings in a list. The separator between the elements of the list is a string object on which join method is called. An Exception ( Type Error ) will be raised, if list contains any non-string element.
# create list object and assign it to variable l l = ['1','2','3','4','5'] # create single space string and assign it to variable separator separator = ' ' # call join() method on separator by passing list l as an argument output = separator.join(l) # print the value of variable output print(output) # second example with '-' as a separator separator = '-' output = separator.join(l) print(output)
You can also check :-
That’s all for How to convert python list to string, you can learn more about tuple . If you liked it, please share your thoughts in comments section and share it with others too.