欢迎光临散文网 会员登陆 & 注册

Python中list字符串拼接

2022-11-01 09:29 作者:永远的修伊  | 我要投稿


1、for循环拼接

word1 = ["abc", "d", "defg"]
word2 = ["abcddefg"]
str1, str2 = "", ""
for i in word1:
   str1 += i
for j in word2:
   str2 += j

print(str1 == str2)


2、使用join函数

str1 = "".join(word1)        

str2 = "".join(word2)        

print(str1 == str2)


3、生成器

word1 = ["abc", "d", "defg"]
word2 = ["abcddefg"]
str1, str2 = "", ""
str1 = "".join(i for i in word1)
str2 = "".join(j for j in word2)

print(str1 == str2)

不积硅步,无以至千里;不积小流,无以成江海


Python中list字符串拼接的评论 (共 条)

分享到微博请遵守国家法律