我必须检查字符串是否包含:字母数字,字母,数字,小写和大写字符

时间:2018-07-13 08:17:49

标签: python string python-3.x

def fun(s):
    for i in s:
        if i.isalnum():
            print("True")
        if i.isalpha():
            print("True")
        if i.isdigit():
            print("True")
        if i.isupper():
            print("True") 
        if i.islower():
            print("True")
s=input().split()
fun(s)

为什么即使在for循环中它也只能打印一次

8 个答案:

答案 0 :(得分:1)

if __name__ == '__main__':
    s = input()
    print(any(c.isalnum()  for c in s))
    print(any(c.isalpha() for c in s))
    print(any(c.isdigit() for c in s))
    print(any(c.islower() for c in s))
    print(any(c.isupper() for c in s))

使用任何函数为python 3更新代码,而无需使用其他函数

答案 1 :(得分:0)

您正在将字符串方法应用于整个单词,而不是单个字符。您没有应用anyall条件来检查每个单词中的任何 all 个字符是否满足每个条件。

例如,每个单词的条件为any

def fun(s):
    for i in s:
        print('\n', i)
        if any(letter.isalnum() for letter in i):
            print('Alnum', True)
        if any(letter.isalpha() for letter in i):
            print('Alpha', True)
        if any(letter.isdigit() for letter in i):
            print('Digit', True)
        if any(letter.isupper() for letter in i):
            print('Upper', True) 
        if any(letter.islower() for letter in i):
            print('Lower', True)

s = input().split()
fun(s)

示例结果:

One test 123

 One
Alnum True
Alpha True
Upper True
Lower True

 test
Alnum True
Alpha True
Lower True

 123
Alnum True
Digit True

答案 2 :(得分:0)

如果要检查整个字符串是否包含那些不同的字符类型,则实际上不必遍历整个字符串。您只能使用any关键字。

def fun(s):
    if any(letter.isalnum() for letter in s):
        print("Is alphanumeric")
    if any(letter.isalpha() for letter in s):
        print("Is alpha")
    if any(letter.isdigit() for letter in s):
        print("Is digit")
    if any(letter.isupper() for letter in s):
        print("Is upper")
    if any(letter.islower() for letter in s):
        print("Is lower")

s=str(input())
fun(s)

答案 3 :(得分:0)

string = input()

print (any(c.isalnum() for c in s))
print (any(c.isalpha() for c in s))
print (any(c.isdigit() for c in s))
print (any(c.islower() for c in s))
print (any(c.isupper() for c in s))

答案 4 :(得分:0)

if __name__ == '__main__':
    s = input()
    print (any([c.isalnum() for c in s]))
    print (any([c.isalpha() for c in s]))
    print (any([c.isdigit() for c in s]))
    print (any([c.islower() for c in s]))
    print (any([c.isupper() for c in s]))

答案 5 :(得分:0)

if __name__ == '__main__':
    s = input()
    print (any([c.isalnum() for c in s]))
    print (any([c.isalpha() for c in s]))
    print (any([c.isdigit() for c in s]))
    print (any([c.islower() for c in s]))
    print (any([c.isupper() for c in s]))

使用 any 关键字,检查字母表中的所有字母并返回 true I 任何一个满足它的字母。

答案 6 :(得分:0)

any() 函数是 Python 中的一个内置函数,它返回 true 如果给定可迭代对象的任何元素 (列表、字典、元组、集合等)为 True,否则返回 False。

if len(s) > 0 and len(s) < 1000:
    print(any([i.isalnum() for i in s]))
    print(any([i.isalpha() for i in s]))
    print(any([i.isdigit() for i in s]))
    print(any([i.islower() for i in s]))
    print(any([i.isupper() for i in s]))

答案 7 :(得分:-1)

if __name__ == '__main__':
    s = input()
    a1,a2,a3,a4,a5=False,False,False,False,False
  
    for i in s:
        a1=True if i.isalnum()==True else a1
        a2=True if i.isalpha()==True else a2
        a3=True if i.isdigit()==True else a3
        a4=True if i.islower()==True else a4
        a5=True if i.isupper()==True else a5
    
    print(a1,a2,a3,a4,a5,sep="\n")