diff --git a/source/rst/python_essentials.rst b/source/rst/python_essentials.rst index 65fc49cf..a54a515e 100644 --- a/source/rst/python_essentials.rst +++ b/source/rst/python_essentials.rst @@ -1050,14 +1050,24 @@ Here's one solution: .. code-block:: python3 def f(string): - count = 0 - for letter in string: - if letter == letter.upper() and letter.isalpha(): - count += 1 - return count + count = 0 + for letter in string: + if letter == letter.upper() and letter.isalpha(): + count += 1 + return count f('The Rain in Spain') +An alternative, more pythonic solution, would be: + +.. code-block:: python3 + + def count_uppercase_chars(s): + return sum([c.isupper() for c in s]) + + count_uppercase_chars('The Rain in Spain') + + Exercise 4 ----------