Using strings in lists and functions - Error: join_strings


Using strings in lists and functions

This is the error message: Oops, try again. join__strings(['x', 'y', 'z', 'a']) returned ['', 'x', 'y', 'z', 'a'] instead of 'xyza'

I do not see what has gone wrong with my code to create ['', 'x', 'y', 'z', 'a'] instead of "xyza" like expected.
Any clarification would be appreciated.
Thank you in advance.


def join_strings(words):
    result = [""]
    for word in words:
    return result


Your result should be String, not List of String (the brackets aren't needed)


it wants you to add to a string, rather than an array, because an array is the result you're getting.
my suggestion is to make result an emptry string, "", and then add each word to result like this:

for word in words: 
    result += word


The instructions say to use .append. I suppose that would work too?


append shouldn't work for strings, only for arrays, let me look at the lesson again.
I checked, and the lesson does not say to use append.
It says Iterate through the words list and append each word to result., but append isn't formatted as a keyword like words and result are. In this case, append is just a verb that means add unto.


Okay, thanks for the help - I got through!



Please help me to correct below code -

n = ["Michael", "Lieberman"]

def join_strings(words):
for item in words:
return result
print join_strings(n)


Traceback (most recent call last):
File "python", line 10, in
File "python", line 6, in join_strings
TypeError: list indices must be integers, not str


Look at the first questions code from pyblaster10469, then read the answer from trigonometrical after that, the answer is there.


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