Use the s.isupper()
method to test if a string is uppercase. You can use indexing to select just the first character.
Thus, to test if the first character is uppercase, use:
if word[0].isupper():
If you want a fast and pythonic approach, use a collections.Counter()
object to do the counting, and split on all whitespace to remove newlines:
from collections import Counter
counts = Counter()
for line in lines: # lines is the text without line breaks
counts.update(word for word in line.split() if word[0].isupper())
Here, word.split()
without arguments splits on all whitespace, removing any whitespace at the start and end of the line (including the newline).