如何使列表元素变成字符串?

如何使列表元素变成字符串?

问题描述:

很抱歉,如果这是一个重复的问题,但是我是Python的新手,所以不确定要问我需要什么.从最基本的意义上来说,我想转一下:

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']

它实际上是:

a = [10, 20, 30]

进入

a = ['102030']

非常感谢您的帮助!

最简单的方法是使用带有空字符串的连接:

The easiest will be using join with empty string:

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

您将获得:

'102030' #a

由于您的列表是整数列表,因此您应该先字符串化"整数:

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)]