0

so.. i want to put all the objects in the list together, and my idea was to create a variable with nothing in it and make a for loop to add the strings to the variable... but it doesn't work :( any help?

I searched for an answer but could not find anything online... I am very noob, so sorry if am wrong.

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

I expected x to be "supercalifragilisticexpialidocious" at the end.

2
  • ''.join(sounds)
    – rafaelc
    Jan 20, 2019 at 22:55
  • You probably forgot an equal sign: x += s.
    – CristiFati
    Jan 20, 2019 at 22:55

1 Answer 1

0

You can use join:

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

x = "".join(sounds)

print(x)

As for your solution, you're missing a =:

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

You get the same thing:

supercalifragilisticexpialidocious
2
  • yeah...that is very useful... but I wonder why the strings cannot be attached to the variable. Jan 20, 2019 at 22:57
  • now i get it... thanks, it was only the +=... sorry :) Jan 20, 2019 at 22:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.