The corresponding value of the ASCII code table, knowing that Ord (' a ') can convert the character ' a ' to the corresponding value on the ASCII code table. Where the number 0-9 corresponds to a code value of 48-57, capital letters A-Z corresponds to 65-90, lowercase letters A-Z corresponds to 97-122.
When judging, be aware that the result of ' 2 ' in range (3) is False, because ' 2 ' is a character, and range (3) contains all numbers. The result of Ord (' 2 ') in range (3) is True.
The code is as follows:
| 123456789101112131415161718192021 |
lst = list(input(‘请输入一行字符,可以是任意字符:‘))iLetter = []iSpace = []iNumber = []iOther = []for i in range(len(lst)): if ord(lst[i]) in range(65, 91) or ord(lst[i]) in range(97,123): iLetter.append(lst[i]) elif lst[i] == ‘ ‘: iSpace.append(‘ ‘) elif ord(lst[i]) in range(48, 58): iNumber.append(lst[i]) else: iOther.append(lst[i])print(‘中英文字母个数:%s‘ % len(iLetter))print(‘空格个数:%s‘ % len(iSpace))print(‘数字个数:%s‘ % len(iNumber))print(‘其它字符个数:%s‘ % len(iOther)) |
The output results are as follows:
Please enter a line of characters, which can be any character: 5000*^*
Number of Chinese and English letters: 7
Number of spaces: 2
Number of digits: 4
Number of other characters: 3
"Python3 Exercise 012" Enter a line of characters, respectively, the number of English letters, spaces, numbers and other characters.