Can some please explain me this code step by step how it execute ? | Sololearn: Learn to code for FREE!

0

Can some please explain me this code step by step how it execute ?

str = "testing for loops" count = 0 for x in str: if(x == 't'): count += 1 print(count)

1/6/2021 3:55:48 PM

Nav

2 Answers

New Answer

+3

for x in str : this cause to copy each character from str into x in each next iterations until no more characters like x='t', next x='e', next...., next ='s' And in loop, if statement check if x=='t' every charecter value of x with t, when matches increments count. So it finding how many 't' s in str. And prints count. as 2.

+1

On line 1 and 2, we set up some basic variables. On line 3, we set up a for loop, that is responsible for going through the characters in str. Inside this for loop, we check if the current character(stored in x) is equal to "t". If so, we add 1 to count. Finally, on line 6 we print the value of count.