Using strings in lists in functions


#1

Please help!
I wrote the following code:

n = ["Michael", "Lieberman"]
def join_strings(words):
result = ""
for i in range(len(words)):
result = result.append(i)
return result
print join_strings(n)

and than I get this error:
Traceback (most recent call last):
File "python", line 7, in
File "python", line 5, in join_strings
AttributeError: 'str' object has no attribute 'append'

I have no idea what's wrong with it...
Thanks!


#2

Strings in python are immutable, change it to a list,

result = []

#3

thank you! It still doesn't work but it is probably the first step.


#4

This is the code I used for that one

n = ["Michael", "Lieberman"]

Add your function here

def join_strings(words):
[indent]result = ""
[indent]for x in words:
[indent][indent]result = result + x
[indent]return result

print join_strings(n)


#5

Do you want a working one?

Try This!

n = ["Michael", "Lieberman"]

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

#6

Thank you very much ghadeer221 that one worked indeed!!
And it's so easy that I understand it. :grinning:
Thanx again!


#7

Your Welcome :smiley:
I'm Glad it helped you!


#8

Another working one as below FYI:

n = ["Michael", "Lieberman"]

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

print join_strings(n)