-1

Let's say I have a function that takes a list and returns a concatenated string.

stringList = ["hello", "world"]
for string in stringList():
    #concatenate all strings in list
return newString

Expected output should be "helloworld"

My original idea was:

for string in stringList():
    string = string + ""
return string

However, this would only return the last element of a list. I'm not sure how to fix this. Also, I cannot use the print function and simply do:

print(string, end="")
Nathan Natindim
  • 147
  • 2
  • 5

2 Answers2

0

You could try the inbuilt function of python strings called join

string = "".join(stringList)
Sharku
  • 1,052
  • 1
  • 11
  • 24
  • 1
    @fen1x Sorry, but this most certainly does answer the question. It may not be a great answer, but the code solves the OP's problem. – Aran-Fey Sep 21 '18 at 09:59
  • @fen1x how about now? It would help if you could specify why you think this does not provide an answer to the OP's question – Sharku Sep 21 '18 at 10:40
0

Just use str.join:

"".join(stringList)
Netwave
  • 40,134
  • 6
  • 50
  • 93