# 16. using strings in list in functions

#1

So i got this right but i still have a hard time i feel understanding how these for loops work exactly. I think i understand whats happening in this code but was wondering if someone could help a noob in Python like me grasp a better understanding of whats really happening. Basically I want to get a solid foundation on how these for loops work exactly? Everytime I think I have the answer it turns out I dont and I have to spend time figuring it out. I think the problem though is that I dont fully understand what the code is doing. Thanks in advance.

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

print join_strings(n)``````

#2

well, this is one simply way to find out:

``````n = ["Michael", "Lieberman"]
for i in range(len(n)):
print i``````

as you can see, `i` gives numbers. range gives you a list of numbers:

``````n = ["Michael", "Lieberman"]
print range(len(n))``````

so the loop loops over the list, getting assigned each item in turn

#3

Thankyou. That makes more sense

#4

#5

Well I probably should ask if I am right first, but after you broke it down a lil I started looking at it more and trying to understand it. Here is what I came up with, please let me know if I am wrong.

n = a list of names

i becomes numbers like you said; it turns the list of n into integers and for every number or every "i" in the range going from 0 to the end of the length of n, it produces a number.

then its prints the length of n.

The loop keeps it going by counting like so i = 0, then comes back around, i = 1, then back around, i = 2, and so fourth.

then result is empty, so then the count of i in words of n gets added to result producing "MichaelLieberman"

Am i right? I may of completely missed it and butchered it haha. I really wanna learn so please correct my thinking if I am wrong anywhere.

#6

you seem to totally understand well done!

#7

Thank you!! Appreciate your help. Have a Merry Christmas!

#8

you're welcome You too!

#9

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