Whether a write function with the python, for determining whether the user entered string of letters and numbers lowercase

First of all, we know that the string to determine the category of functions are:

String .isalnum () All characters are numbers or letters, is true returns Ture, otherwise False.

String .isalpha () All characters are letters, is true returns Ture, otherwise False.

String .isdigit () All characters are numeric, true return Ture, otherwise False.

String .islower () are all lowercase characters, true return Ture, otherwise False.

String .isupper () All characters are uppercase, true return Ture, otherwise False.

String .istitle () all words are capitalized, is true returns Ture, otherwise False.

String .isspace () all characters are blank characters, true return Ture, otherwise False.

But now we can put it another way to judge, to "determine whether the string entered by the user lowercase letters and numbers" as a small problem, a function to write your own judgment:
here we have to use ASCII code to determine string, and 48-57: digital 0-9,97-122: lowercase letters, can Baidu more detailed ASCII code.

#编写一个函数,判断输入的字符串是否由小写字母和数字组成
char = input("请输入一个需要判断的字符串:")
lenght = len(char)
j = 0       #与字符串长度作比较条件
a = 0     #用于记录在ASCII码内并累加,最后和字符串长度一致则认为是以下结论
while j <= lenght :
    for i in char :
        num = ord(i)    #ord()把字符串转为ASCII码
        if num >= 48 and num <= 57 or num >= 97 and num <= 122 :
            a += 1      #在范围内就累加
        j += 1
if a == j :       #判断累加结果与字符串长度
    print("该字符串由小写字母和数字组成")
else:
    print("该字符串包含大写字母或者是符号!")
Published 29 original articles · won praise 8 · views 4671

Guess you like

Origin blog.csdn.net/weixin_43716338/article/details/101145891