16. Using strings in lists in functions


#1

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?


#2

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"]

#3

Ohhh yeah I forgot that. Thank you again.


#4

Welcome. Glad to help. :slight_smile:


#5

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


#6

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


#7

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


#8

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!


#9

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:


#10

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


#11

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)


#12

the above code I tried too but it shows error.

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


#13

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)

#14

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.


#15

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)

#16

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.


#17

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.


#18

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