Count the number of alphanumeric spaces in a string

Source: Internet
Author: User

This article is mainly implemented: Enter a string, calculate its number of letters, numbers, spaces and other symbols

Code:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
# Author by LH

R=raw_input ("Please enter the string you want to evaluate:")

def func (s):
All_alphanum=0 #初始化字母的总数为0
All_othersnum=0 #初始化其他字符总数为0
All_spacenum=0 #初始化空格的总数为0
All_digitnum=0 #初始化数字的总数为0
For I in S:
If I.isdigit ():
All_digitnum+=1
Elif I.isspace ():
All_spacenum+=1
Elif I.isalpha ():
All_alphanum+=1
Else
All_othersnum+=1
Return (All_digitnum,all_spacenum,all_alphanum,all_othersnum)

Ret=func (R)
Print ret

Operation Result:

Count 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.