Reputation: 1
How should I convert list elements to string using Python?
For example I have a list that looks like this:
[1, 2, 3, 4, 5]
And I want it to look like this:
['1', '2', '3', '4', '5']
Upvotes: 0
Views: 57
Reputation: 496
Use a one-liner to iterate over all elements it an Iterable
and use str(element)
to cast the element as a string
new_list = [str(i) for i in old_list]
Upvotes: 1
Reputation: 1
def listToString(a):
li = []
for i in a:
li.append(str(i))
return li
We can input the list in the function.
Upvotes: 0