I am trying to write code to count the number of upper case letters in a string using Python. I have written the following but instead of the code returning the number of Upper Case letters it returns []? An help would be gratefully recieved.

import re

while True:
    strPassword = input("Please enter your password: ")
    length = len(strPassword)
    if length < 6 or length > 12:
        print ("It is not the correct length")
    else:
        break
print ("Password OK")

upper = 0
lower = 0
number = 0

print (re.findall("A-Z", strPassword, 0))

[] is an empty list. re.findall returns a list.
What you've done is miss a bit of re syntax.
Here's a tip -

>>> re.findall('A-Z','A-Z')
['A-Z']
>>> re.findall('A-Z','A-ZsjdjoA')
['A-Z']
>>> re.findall('[A-Z]','A-Z')
['A', 'Z']
>>> re.findall('[A-Z]','A-ZsjdjoA')
['A', 'Z', 'A']

See the difference? ;)

Enalicho has give some tip.
Here a couple more.
Always use r(raw_string) for regex expression.
If you want to count,just use len() because re.findall() is returning a list.
Here is a example run.

>>> import re
>>> user_input = 'aaaaBBBCC'
>>> re.findall(r'A-Z', user_input)
[]
>>> #Missing []
>>> re.findall(r'[A-Z]', user_input)
['B', 'B', 'B', 'C', 'C']
>>> #Count
>>> len(re.findall(r'[A-Z]', user_input))
5
>>>

Edited 5 Years Ago by snippsat: n/a

Comments
Good call; I shouldn't have left out the r
This question has already been answered. Start a new discussion instead.