Count the uppercase letters in a string with Python Count the uppercase letters in a string with Python python python

Count the uppercase letters in a string with Python


You can do this with sum, a generator expression, and str.isupper:

message = input("Type word: ")print("Capital Letters: ", sum(1 for c in message if c.isupper()))

See a demonstration below:

>>> message = input("Type word: ")Type word: aBcDeFg>>> print("Capital Letters: ", sum(1 for c in message if c.isupper()))Capital Letters:  3>>>


You can use re:

import restring = "Not mAnY Capital Letters"len(re.findall(r'[A-Z]',string))

5


Using len and filter :

import stringvalue = "HeLLo Capital Letters"len(filter(lambda x: x in string.uppercase, value))>>> 5