如何向空变量添加字符串以生成长单词?

2024-10-10 00:22:32 发布

您现在位置:Python中文网/ 问答频道 /正文

所以。。我想把列表中的所有对象放在一起,我的想法是创建一个没有任何内容的变量,并创建一个for循环,将字符串添加到变量中。。。但没用:(有什么帮助吗?你知道吗

我搜索了一个答案,但在网上找不到任何东西。。。我很抱歉,如果我错了。你知道吗

sounds = ["super", "cali", "fragil", "istic", "expi", "ali", "docious"]
x=""
for s in sounds:
    x+s
print(x)

我希望x在最后是“超级资格的专家”。你知道吗


Tags: 对象字符串答案in内容列表forali
1条回答
网友
1楼 · 发布于 2024-10-10 00:22:32

您可以使用join

sounds = ["super", "cali", "fragil", "istic", "expi", "ali", "docious"]

x = "".join(sounds)

print(x)

至于您的解决方案,您缺少一个=

sounds = ["super", "cali", "fragil", "istic", "expi", "ali", "docious"]
x=""
for s in sounds:
    x += s
print(x)

你会得到同样的结果:

supercalifragilisticexpialidocious

相关问题 更多 >

    热门问题