THE PYTHON range() FUNCTION
In this section you will learn about the Python range() function that you can use to iterate through a list, as shown here:
>>> for i in range(0,5): ... print i ... 0 1 2 3 4
You can use a for loop to iterate through a list of strings, as shown here:
>>> x ['a', 'b', 'c'] >>> for w in x: ... print w ... a b c
You can use a for loop to iterate through a list of strings and provide additional details, as shown here:
>>> x ['a', 'b', 'c'] >>> for w in x: ... print len(w), w ... 1 a 1 b 1 c
The preceding output displays the length of each word in the list x, followed by the word itself.
Counting Digits, Uppercase, and Lowercase Letters
Listing 3.4 displays the contents of the Python file CountCharTypes.py that counts the occurrences of digits and letters in a string.
LISTING 3.4: Counter1.py
str1 = "abc4234AFde" digitCount = 0 alphaCount...