Using Strings in Lists in Functions


Using Strings in Lists in Functions

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

Replace this line with your code. 
words = ["Michael", "Lieberman"]
# Add your function here

def join_strings(words):
    for i in words:
        return result


Be sure that return is not inside the loop. It should line up with for.


it is still an error


It's my nature to always start with the obvious and work from there. The above line is where your error is.


Sometimes it is easier if we work with named variables so we can visualize what is happening.

for word in words:


now it says that: Oops, try again. join_strings(['x', 'y', 'z', 'a']) resulted in an error: 'str' object has no attribute 'append'


Now we are getting somewhere. Since we cannot append a string object (strings are immutable) how else could we do it? Hint: Think of i = i + 1 for example.


4 posts were split to a new topic: Why result.append(words) wouldn't work


2 posts were split to a new topic: Still getting the same error "Oops, try again

Still getting the same error "Oops, try again

I'm not sure if this helps, but I sort of saw "append each word" as either a trick question or a misleading term. I ended up using the following code, which achieve the same result, using concatenation rather than the .append() function.

    result = ""
    for i in range(len(words)):
        result = result + words[i]

I figured it out when @mtf noted that strings are immutable and hence cannot be appended.


A post was split to a new topic: How to get the list to combine with a string


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