16.Using strings in lists and functions

  1. Using lstrings in lists and functions

n = [“Michael”, “Lieberman”]
def join_strings(words):
result = ""
for i in words:
result = result + i
return result

print join_strings(n)

I get this error message:
join_strings([‘x’, ‘y’, ‘z’, ‘a’]) returned ‘x’ instead of ‘xyza’

i have no idea why this is not working… please could someone help me.

Thank you.


Hi @hanfack :smiley:
I tested your source code out and everything works great. It must be a bug in Codecademy’s Python interpreter.
But my suggestion would be is to either refresh the page or report the bug.
Orrr, you could change result = result + i to:
That doesn’t really have an effect on the overall program, but makes the code look more concise.

Good luck!


Make sure that the indentation of your ‘return result’ is in the function, but not inside the for loop. If the ‘return result’ is inside the for loop then the only the only thing that will be returned is the first index of the list when it passed through the function. I have had this problem on different lessons.


Thanks - Yes it seemed to work in the end but a little buggy sometimes it seems!


n = [“Michael”, “Lieberman”]
def join_strings(words):
result = ""
for i in range(len(words)):
result = result.join(words)
return result

print (join_strings(n))

is giving me output as MichaelMichaelLiebermanLieberman. I want it just once.


I think your joining the list and not the individual words. “i” is the variable storing the individual item from the list. words=n,“i”=n[item], I don’t know how to make your code work with .join, I tried for a while until the interpreter crashed, but if someone else has an answer , I would love to know.


Hey I was able to do it.

n = [“Michael”, “Lieberman”]

Add your function here

def join_strings(words):
result = ""
for w in range(len(words)):
result += words[w]
return result

print join_strings(n)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.