idx = 0 numchars = 0 astr = "this is a string to use in the example" for chr in astr: numchars += 1 if chr == 'i': idx = idx + 1 in python

main.py
idx = 0 
numchars = 0 
astr = "this is a string to use in the example" 
for chr in astr:
    numchars += 1 
    if chr == 'i':
        idx = idx + 1
149 chars
8 lines

This code initializes the variables idx and numchars to 0, and a string variable astr. Then, the code loops through each character in the string astr. For each character, numchars is incremented by 1. If the character is an 'i', then idx is incremented by 1.

At the end of the loop, idx will contain the number of 'i' characters in the string astr, and numchars will contain the total number of characters in the string astr.

gistlibby LogSnag