Python将数组合并为一个字符串

时间:2016-04-07 16:19:46

标签: python

我希望能够将可变长度数组合并为单个字符串,例如:

var1 = ['Hello',' ','World','!']

变为:

var2 = 'Hello World!'

并且第一个数组可能大于4.感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

  

str.join()

     

返回一个串联的字符串   可迭代迭代中的字符串。元素之间的分隔符是   提供此方法的字符串。

列表项将与提供的字符串连接(连接)。使用''将加入所有没有字符的项目。

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> var1 = ['hello', ' ', 'world', '!']
>>> ''.join(var1)
'hello world!'
>>> '     '.join(var1)
'hello           world     !'
>>> '*%*%*'.join(var1)
'hello*%*%* *%*%*world*%*%*!'
>>> 
相关问题