Python Learning: Determining the number of alphanumeric spaces in a string

Source: Internet
Author: User

Enter a line of characters to count the number of letters, spaces, numbers, and other characters that contain English. Count the number of English alphabetic characters ' ' # -*- coding: utf-8 -*-import re# regular expression matching def ismathc (Src,pat):     pattern = re.compile (PAT)     result = re.match (PATTERN,SRC)     if result == None:         return 0    else:        return  1# determine the type of individual character: Def chargetype (character) the three elements in the:    #  array represent English letters, numbers, and spaces, respectively      type_num = [' [a-z]|[ A-z] ',  ' \d ',  ' \s ']    #  to determine whether the English alphabet     if ismathc ( Character, type_num[0])  == 1:        return 1     ELIF ISMATHC (character, type_num[1])  == 1:         return 2     elif ismathc (character, type_num[2])  == 1:         return 3    else:        return  4def getcharnum (str):     count = []    for i  in range (4):         count.append (0)      Print (Len (count))     for i in range (Len (str)):         if chargetype (Str[i]) ==1:             count[0]+=1        elif chargetype (Str[i]) ==2:             count[1]+=1         elif chargetype (Str[i]) ==3:             count[2]+=1        else:             count[3]+=1    return countstr= ' asc ss./ 124 ' Print ( Getcharnum (str))


Python Learning: Determining the number of alphanumeric spaces in a string

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.