python判断字符串是否包含大写字母_检查字符串中是否存在大写字母

时间:2025-02-09 07:29:29

可以将any与生成器一起使用,以测试字符串是否包含大写字母testString = "abjKcf"

print(any(() for x in testString)) #true

好的解决方案

至于解决问题的方法,欢迎来到生成器表达式和断言的世界

^{pr2}$

非常难看的解决方案(按要求)

基本上你设置了一些布尔函数,并尝试通过循环每个字符并检查它是否满足某些条件来反驳它们while True:

testString = input()

allAlphaNumeric = True

oneCapital = False

oneLowerCase = False

oneDigit = False

for letter in testString:

if not ():

oneAlphaNumeric = False

if ():

oneCapital = True

if ():

oneLowerCase = True

if ():

oneDigit = True

numberAtStart = testString[0].isdigit()

numberAtEnd = testString[-1].isdigit()

if allAlphaNumeric and oneCapital and oneLowerCase and oneDigit and not numberAtEnd and not numberAtStart:

break

if not 8 <= len(testString) <= 15:

print("String must be between 8 and 15 characters")

if not allAlphaNumeric:

print("Your string must be alphanumeric!")

if not oneCapital:

print("Your string must contain at least one capital letter")

if not oneLowerCase:

print("Your string must contain atleast one lowercase letter")

if not oneDigit:

print("Your string must contain atleast one digit")

if numberAtStart:

print("You cannot have a number at the start")

if numberAtEnd:

print("You cannot have a number at the end")