Using strings in lists in functions



i don;t know what's wrong with my code still after checking all the similar topic...
could anyone help me ?


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

By appending, You're not joining the strings (items) of list,You're again appending them in a new list and returning a list having same items as original.

You have to return final string which is nothing but concatenation of input list items.

further hint**
one approach can be...
going through instruction...


since append function works for lists not strings


instead of result.append(item) try result += item


The method by "result = result + item" works, but I still think there is another way to go.

However it stills off the road...


did anyone answer this?
i see solutions for using the for loop but none with the range method.

did you solve this with this method?


were we never supposed to use .append() in this exercise?


i don't think we're supposed to use results.append() here... since append doesn't work on strings. i'm finding CodeCademy's hints to be very misleading at times. but i'm sure it's for our benefit!


Yep the general rule is not to use append for string - use concatenation instead.
Append is for lists only.


.append() method is defined for list not for string,int,float,tuple or dictionary data types.


n = ["Michael", "Lieberman"]

Add your function here

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

it worked like this


You have to treat result and the inputted thing as separate entities--they follow different rules so you have to address them differently when combining them


Hey, using the loop solution is pretty straight forward! After noticing your question, I thought I'll give it a try with using the range method. Here's my solution:

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

def join_strings(words):
    result= ""
    for i in range(0, len(words)):
        result += "%s" % words[i]
    return result

print join_strings(n)

It works. Since the range method helps us store the indices of the list in variable the i, we need to use the list[index] method to insert the values at index 0 & index 1 i.e words[i] in the above code.

Hope this helps :slight_smile:


Can someone explain to me why this code is incorrect?

I keep getting the following error:
"Oops, try again. flatten([[1, 2], [3, 4]]) returned [1, 2, 3, 4, 5, 6, 7, 8, 9] instead of [1, 2, 3, 4]"


Disregard the Y I was messing with some other iterations.


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