Strange behavior for (for loop) in Python ? simple but tricky! ( search a char in phrase and prints its index)


#1

I edited slightly the code to print the index of "i" in the variable word which acts as list of character
If character is not "i" , it will print the character itself ( not the index)
word = "Programming is fun!"
#

The output is strange!

P
r
o
g
r
a
m
m
8 >> This is correct
n
g

8 >> This is Incorrect !! should 12 instead of 8
s

f
u
n
!


#2

if you are using find method, i should say this method just find first occured. if you want to show better to write a for loop for crossing string (in char by char) but in range(0, stringlen)...

then use an if to check what is character. if is 'i' print counter variable.


#3

Yes , you're right .
Got fixed as you recommded to use range


#4

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