Using strings in lists in functions


#1

I'm curious to know if we can get the same result without using the range() function. Is it possible to use 'for word in words"? or do we have to use range? This relates to how we are modifying the list? It's not clear to me.


#2

Have you tried running that code? It will answer your question as to whether it is possible.
And it is possible to do for word in words: as seen here: link

There are many ways to go through each item of a list and if there are two ways, we don't always have to do it one way. It's like how there are many ways to solve a problem!

You're not really modifying the list as you are just going through it and adding words to a newstring. Nothing is done to the list.


#3

Thanks datfatcat. Yeah, I see where I was going wrong. I was treating string like integer.

result += word, is what I should do.

Got it.


#4

result += word

is the same as

result = result + word

So you weren't wrong in using the latter :slight_smile:


#5

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