for loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

for loops

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

26th Oct 2021, 1:07 PM
Nida Shirin
Nida Shirin - avatar
2 Answers
0
thankyou very much..but i was looking for the explanation of python for beginners 32.1 lesson. I appreciate your prompt reply though😇
26th Oct 2021, 2:47 PM
Nida Shirin
Nida Shirin - avatar
0
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???
26th Oct 2021, 4:07 PM
Barnik Roy
Barnik Roy - avatar