# 16/18 same code different outcome

#1

So, for this exercise you are supposed to combine strings, and I get that. However, the method used in the for function works only for one case. I used:

``````for word in words:

result.append(word)``````

and then return result. However, this version is not working, but I see most people using this one instead, and it works.

``````for i in range(len(words)):
result = result + i``````

and then return result. What is the difference between the two, and why is it that only the second one works? Thanks in advance

#2

sorry, what I meant was

``````for i in range(len(words)):

result = result + words[i]``````

My question is, why do we have to use range(len(words)) and why do we do we have to do result = result +words[i] instead of result= result + i. Isn't the function itenerating over every item? If it does, why can we not just do result = result + i?

Think about it. If the list words has its first item as "Hello" then we are telling the computer to go to words["Hello"] instead of words[0]. Unless the function goes over each item as a number, for example i would be 0, then 1, then etc.? Please just explain this to me, im so confused.

#4

Strings are immutable, (just like tuples) so the append() method will not work on a string,So the only way to change a string is to use the

string = string + another_string

method. Keep in mind tuples, function the same way. Hope it helped.

Cheers.

#5

Thanks for asking this question! I was racking my brain trying to get around the error. I think the language in the exercise is misleading. They should probably not use the word append, when talking about strings.

#6

Hi guys, hope it helps ,try this one:

n = ["Michael", "Lieberman"]

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

print join_strings(n)

#7

Try this:

n = ["Michael", "Lieberman"]