Python:将列表转为字符串的3种方法

#一天一个Python小技巧#

将列表转为字符串:

1、使用for循环

testlist = ['h','e','l','l','o']
teststr =''
for i in testlist:
    teststr += i 
print(teststr)

2、join方法:

testlist = ['h','e','l','l','o']
teststr = "".join(testlist)
print(teststr)

3、reduce方法:

from functools import reduce
testlist = ['h','e','l','l','o']
teststr = reduce(lambda x, y: x+y, testlist)
print(teststr)
PS C:\Users\farew> & D:/ANACONDA/python.exe c:/Users/farew/Desktop/Untitled-1.py
hello

 

    原文作者:Verney7
    原文地址: https://blog.csdn.net/Verney7/article/details/116995264
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞