如何在列表内串联字符串

时间:2019-06-08 16:54:10

标签: python-3.x list concat

我有一个字符串列表,我想将它们组合在一起。

list = ["name", "lastname", "year"]
var = ''.join(list)
print(list)

我希望输出为:

   nameyear
   lastnamename
   lastnameyear
   yearname
   yearlastname

1 个答案:

答案 0 :(得分:0)

尝试一下:

words = ["name", "lastname", "year"]

for i, word1 in enumerate(words):
    for j, word2 in enumerate(words):
        if i != j:
            print(word1 + word2)

输出:

namelastname
nameyear
lastnamename
lastnameyear
yearname
yearlastname

此外,不要使用list作为变量名,因为它是python关键字。