16. Using strings in lists in functions


#1

I have tried finding the answer to this one but all of them do not use the append function. Does anyone have a version of the code with append that I could see?
BTW this is what I did and I don't know why it doesn't work:

n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
    result = ""
    for i in range(len(words)):
        result.append(words[i])
    return result


print join_strings(n)

#2

Hi @jacobisnotarobot ,

Because the code that you posted is not formatted, it is difficult for other users to read and debug it. After code has been pasted into the editing window for posting, you can format it by selecting it, and then by clicking the </> button above the editing area. Alternatively, you can place three backquotes on the line before the code and three backquotes on the line after the code. This will enable us to see important details, such as the indentation and underscores. If you use the backquotes, your code will be color-coded, making it especially easy to read.

append is a list method, and not a string method. Therefore, you need to begin by assigning an empty list to result. Then, this will be acceptable inside the loop ...

result.append(words[i])

After the loop, you can do this to convert the contents of the list to a string ...

return "".join(result)

#3

I have the same problem!
my current code is

def join_strings(words):
    result = ""
    for word in words:
        result.append(word)
    return result

i've also found a website that explains the diff between .append and += http://stackoverflow.com/questions/725782/in-python-what-is-the-difference-between-append-and


#4

Thank you, yeah I didn't realise until afterwards that my indentation was lost...sorry I'll do what you said next time