for loops | Sololearn: Learn to code for FREE!


for loops

i did not clearly understand the iterating strings with for loops in python(for beginners). Can someone pls explain that part?

10/26/2021 1:07:18 PM

Nida Shirin

2 Answers

New Answer


thankyou very much..but i was looking for the explanation of python for beginners 32.1 lesson. I appreciate your prompt reply though😇


as we come to learn, we evidence, 'strings' are "indexed" python objects. that's, each "character"/element of your string has a specific index (or you may call it "character log" for that string...except the log starts from 0 rather than 1 😅) s = "keep going!" print(s[-1]) # prints the last element # prints ! now there, our "for loop" comes into action. it takes each and every "character" of that string and runs commands you want to apply on those elements. s = "keep going!" l = [] for char in s: # takes elements of s one at a single step l.append(element) if char == ' ': break # now moves to the next charcter of s print(l) # prints ['k', 'e', 'e', 'p'] does it help???