16. Using strings in lists in functions

Hello, I’m doing “Lists and functions” more specifically 16. Using strings in lists and functions and I was able to do the exercise but I have a question in the method (I want to do it in another way).


n = ["Michael", "Lieberman"]
# Add your function here

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

print join_strings(n)

This is what I have and it works. But instead of this:

result += item

I want something like:

result.append(item)

since this was taught in previous lessons. But it doesn’t work. What’s the correct way of doing it with this method?

1 Like

Hey there again! You append things to a list, not a string. So for your method to work, the variable “results” has to be a list. If you do that, however, then the function would give back exactly what it started with in the first place then.

Results as a string:

results = "MichealLieberman"

Results as a list:

results = ["Michael", "Lieberman"]
10 Likes

Ohhh yeah I forgot that. Thank you again.

Welcome. Glad to help. :slight_smile:

1 Like

this message is so it stays open so people can use this as this is very helpful thank you

1 Like

how would you do it the other way where you would use
for i in range(words):

instead of for item in words:
?
Thanks a bunch

Don’t quite get what you mean…can you elaborate on that? And possibly show how you want to code it, and why?

Oh Yeah definitely. So in the code you used:
For item in words:

I was wondering if there was a way to use:
For i in range():
and still get the correct answer. Not sure if that helped you at all or not. But Thanks!

1 Like

I suppose we could say:

for i in range(len(word))

More information about the range method here:
https://docs.python.org/2/library/functions.html#range

I’ll try tinkering with my code and check it out, not sure if it will work though. :slight_smile:

People can always see it if they search through the Forum or can see it on similar topics. No need to keep it open.

Sjiang - I’ve copied my code below using the range() version so you can see what worked for me. The words[item] part is equivalent to the list[i] part they used in the example. Cheers, Alison.

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

print join_strings(n)

the above code I tried too but it shows error.

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

looks similar to mine and it works…

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

You have to indent the return and print statements in the right way or Python won’t read it properly. Follow the indentation that arcjumper84571 has shown in his/her answer.

I am interested in using the .append() as the exercise requires but I am stuck. Only the following code works:

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


print join_strings(n)

You append things to a list, not a string. In order to use the append method the variable results would need to be an empty list.

I have identical code to this and it doesn’t work.
never mind I changed words to word and it worked even though, in the instructions it tells you that the required argument is words.

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