如何检查Python中的字母是否在A-Z和a-z范围内?

问题描述 投票:0回答:1

所以这个单词必须有两个A-Z字符和a-z字符。我使用

.isaplha()
函数检查小写字母,那么如何处理大写字符?

是的,我唯一想到的是

CapiChar=['A',......'Z']
和if char in
CapiChar COunter=counter+1

python string character
1个回答
0
投票

.isalpha()
检查小写和大写。您应该使用
.isupper()
.islower()
方法:

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=}")

这应该打印:

lowercase_count=4, uppercase_count=2
© www.soinside.com 2019 - 2024. All rights reserved.