16. Using Strings in Lists in Functions




Error message:
Oops try again. join_strings(['x', 'y', 'z', 'a']) returned 'xyza' instead of 'xyza'

It's adding my strings together without spaces, but i'm not sure how to include the space. Please can someone help?

n = ["Michael", "Lieberman"]

def join_strings(words):
    result = " " 
    for n in words:
        print n
        result += n
    return result

print join_strings(n)


Don't print inside the function. Just concatenate and return the result.

Also, initialize result as an empty string.


Ok, thank you - doesn't the line result = " " mean that i've initialised it? If it doesn't, then i'm not sure i understand how to initialise. thanks


You did initialize it, but with a space character which is throwing off the result. Use the empty string, "" or ''.


I see! Fixed now, thank you so much!


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