"Python3 Exercise 012" Enter a line of characters, respectively, the number of English letters, spaces, numbers and other characters.

Source: Internet
Author: User
Tags ord

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.