Devang
Devang

Reputation: 1

How to convert list elements to string using Python?

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

Answers (2)

d-man
d-man

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

Devang
Devang

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

Related Questions