【问题标题】:How can i add strings to an empty variable to make a long word? [duplicate]如何将字符串添加到空变量以生成长单词? [复制]
【发布时间】:2019-06-14 08:35:38
【问题描述】:

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

我搜索了答案,但在网上找不到任何东西...我很菜鸟,如果有错误,请见谅。

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

我预计 x 最后会是“supercalifragilisticexpialidocious”。

【问题讨论】:

  • ''.join(sounds)
  • 你可能忘记了一个等号x += s

标签: python string variables add


【解决方案1】:

你可以使用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

【讨论】:

  • 是的……这很有用……但我想知道为什么字符串不能附加到变量上。
  • 现在我明白了...谢谢,这只是 +=... 抱歉 :)
猜你喜欢
  • 2016-03-26
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 2020-01-29
  • 2023-03-12
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多