Why This code gives an error " String index out of range " ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why This code gives an error " String index out of range " ?

print (" input string") str1=input() count=0 for i in range(0,len(str1)): if(ord(str1[i]+1 == ord(str1[i+1])): count +=1 else: count =0 print(count)

28th Sep 2019, 3:39 PM
Pushkar Singh Bisht
Pushkar Singh Bisht - avatar
1 Answer
+ 1
Your loop goes until len(str1). So eventually str1[i] will be the last letter. But even then you try to access str1[i+1] which is one slot beyond your string.
28th Sep 2019, 3:52 PM
HonFu
HonFu - avatar