Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Quick recap
9. Iterating over strings
Indexing and slicing


Great! To iterate over every character in a string, we can use a for loop:

username = input('To register, enter a username without digits: ')
for character in username:
  if character.isdigit():
    print(character + ' is a digit!')

The code above checks every single character in the string. If any character is a digit, a warning is shown.

Incidentally, we used the isdigit() function here. We'll explain it in detail in the next part of the course.


The letter e is said to be the most frequent letter in the English language. Count and print how many times this letter appears in the poem below:

John Knox was a man of wondrous might,
And his words ran high and shrill,
For bold and stout was his spirit bright,
And strong was his stalwart will.

Kings sought in vain his mind to chain,
And that giant brain to control,
But naught on plain or stormy main
Could daunt that mighty soul.

John would sit and sigh till morning cold
Its shining lamps put out,
For thoughts untold on his mind lay hold,
And brought but pain and doubt.

But light at last on his soul was cast,
Away sank pain and sorrow,
His soul is gay, in a fair to-day,
And looks for a bright to-morrow.

(Source: "Unidentified," in Current Opinion, July 1888)

Stuck? Here's a hint!

We store the poem in a string variable. Create another variable to count the instances of e. Then, iterate over the string and check each letter—if it's an e, increase the counter.