用python写一个函数判断输入的字符串是由大写字母、小写字母和数字组成且至少含有一个大写字母而且至少含有一个小写字母而且至少含有一个数字。
def is_valid_string(s):
has_upper = False
has_lower = False
has_digit = False
for c in s:
if c.isupper():
has_upper = True
elif c.islower():
has_lower = True
elif c.isdigit():
has_digit = True
return has_upper and has_lower and has_digit
# test
print(is_valid_string("Abc123")) # True
print(is_valid_string("ABC123")) # False
print(is_valid_string("abc123")) # False
print(is_valid_string("Abc")) # False
print(is_valid_string("123")) # False
``
原文地址: https://www.cveoy.top/t/topic/fDNL 著作权归作者所有。请勿转载和采集!