Noah Sprenger
Noah Sprenger

Reputation: 61

How to make list elements into string?

I'm sorry if this is a duplicate question, but I am new to Python and not quit sure how to ask for what I need. In the most basic sense, I would like to turn this:

a = ['10', '20', '30']

Edit:

It actually:

a = [10, 20, 30]

into

a = ['102030']

Thank you so much for your help!

Upvotes: 1

Views: 1662

Answers (4)

Jared Goguen
Jared Goguen

Reputation: 9008

Late answer after edit:

[''.join(map(str, a))] # ['102030']

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71471

Try this:

a = ['10', '20', '30']

print [''.join(a)]

Upvotes: 0

Ian
Ian

Reputation: 30823

The easiest will be using join with empty string:

a = ['10', '20', '30']
a = ''.join(a) #use a as result too

You will get:

'102030' #a

Edit:

Since your list is list of integer, you should "stringify" the integers first:

a = [10, 20, 30]
a = ''.join(str(x) for x in a)

or, if you want to put the single result as string to, then enclose the final result with [...]:

a = [''.join(str(x) for x in a)]

Upvotes: 1

snowflake
snowflake

Reputation: 951

How about,

a = ''.join(a)
a = [a]  # if you want to put the string into a list

same question here: How to collapse a list into a string in python?

Upvotes: 2

Related Questions