0

So the word must have two A-Z characters and a-z characters. I used the .isalpha() function to check lower case then how do I do with uppercase characters?

yeah the only thing on my mind is CapiChar=['A',......'Z'] and if char in CapiChar: counter=counter+1

Selcuk
  • 57,004
  • 12
  • 102
  • 110

1 Answers1

0

.isalpha() checks for both lowercase and uppercase. You should use .isupper() and .islower() methods:

my_word = "FooBar"

lowercase_count = len([c for c in my_word if c.islower()])
uppercase_count = len([c for c in my_word if c.isupper()])

print(f"{lowercase_count=}, {uppercase_count=}")

This should print:

lowercase_count=4, uppercase_count=2
Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • It misses the constraint to ascii range, if we have a string "FoöB" the code above would count ö, but question has a restriction to Basic Latin letters. So either the string module and ascii_lowercase() etc, or extend the list comprehension: len([c for c in my_word if c.islower() and c.isascii()]) etc. – Andj Aug 12 '23 at 08:07
  • Where did you see that restriction? The OP was originally using `.isalpha()`, so apparently they are either happy with non-ASCII letters, or it is not relevant. – Selcuk Aug 13 '23 at 08:53
  • the question was "so the word must have two A-Z characters and a-z characters." Yes the OP used `.isalpha()` but A-Z characters and a-z characters." Yes the OP used `.isalpha()` will not provide the results he wants unless the input string is already restricted to ASCII characters (but that isn't explictedly stated in the question. But personally I see little use for `.isalpha()`. I tend to be more interested in word forming characters. `.isalpha()` is way too restrictive for my needs. – Andj Aug 13 '23 at 10:02
  • We don't know what the use case is, we do not no what a representative string looks like, we don't know what characters may or may not be in the string. Probability is that its like most python code and has a limited character repertoire, but that is a guess. All we know is that there must be two lowercase basic Latin characters and two uppercase basic Latin characters. – Andj Aug 13 '23 at 10:12