zl程序教程

您现在的位置是:首页 >  Python

当前栏目

Python怎么将列表转字符串

2023-03-20 15:26:16 时间

Python怎么将列表转字符串

Python怎么将列表转字符串

例子:

将列表 temp_list = ['h', 'e', 'l', 'l', 'o'] 转换成字符串'hello',代码如下:

temp_list = ['h', 'e', 'l', 'l', 'o']
result = ''.join(temp_list)
print(result)    

hello

推荐学习《Python教程》。

字符串对象的方法join其描述如下:

大概意思是:s.join(iterable)是将括号内的迭代对象(如列表)使用s字符串作为链接将迭代对象中的元素拼接成一个字符串,返回该字符串。

def join(self, iterable): # real signature unknown; restored from __doc__
        """
        S.join(iterable) -> str
        
        Return a string which is the concatenation of the strings in the
        iterable.  The separator between elements is S.
        """
        return ""