zoukankan      html  css  js  c++  java
  • python 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

    一、参考解法:

    s =input('请输入字符串:')
    dic={'letter':0,'integer':0,'space':0,'other':0}
    for i in s:
        if i >'a' and i<'z' or i>'A' and i<'Z' :
            dic['letter'] +=1
        elif i in '0123456789':
            dic['integer'] +=1
        elif i ==' ':
            dic['space'] +=1
        else:
            dic['other'] +=1
            
    print('统计字符串:',s)
    print(dic)
    print('------------显示结果2---------------')  
    for i in dic:
        print('%s='%i,dic[i])
    print('------------显示结果3---------------')  
    for key,value in dic.items():
        print('%s='%key,value)
    

    二、参考解法:

    tmpStr = input('请输入字符串:')
    alphaNum=0
    numbers=0
    spaceNum=0
    otherNum=0
    for i in tmpStr:
        if i.isalpha():
            alphaNum +=1
        elif i.isnumeric():
            numbers +=1
        elif i.isspace():
            spaceNum +=1
        else:
            otherNum +=1
    print('字母=%d'%alphaNum)
    print('数字=%d'%numbers)
    print('空格=%d'%spaceNum)
    print('其他=%d'%otherNum)
    

    三、参考解法:

    InPut = input('请输入字符串:')
    letters  = [ ]
    spaces = [ ]
    digits   = [ ]
    others = [ ] 
    for i in iter(InPut):
        if i.isalpha():
            letters.append(i)
        elif i.isspace():
            spaces.append(i)
        elif i.isdigit():
            digits.append(i)
        else:
            others.append(i)
    print('''
    字母: {}, 个数: {}
    空格: {}, 个数: {}
    数字: {}, 个数: {}
    其他: {}, 个数: {}'''
    .format(letters, len(letters), spaces, len(spaces), digits, len(digits),others, len(others)))
    

    四、参考解法:

    使用正则表达式 re.findall()

    import re
    s = input('请输入一串字符:')
    char=re.findall(r'[a-zA-Z]',s)#以列表类型返回全部能匹配的子串
    num=re.findall(r'[0-9]',s)
    blank=re.findall(r' ',s)
    chi=re.findall(r'[u4E00-u9FFF]',s)#汉字的Unicode编码范围
    other = len(s)-len(char)-len(num)-len(blank)-len(chi)
    print('字母',len(char),'
    数字',len(num),'
    空格',len(blank),'
    中文',len(chi),'
    其他',other)

     五、参考解法:

    使用正则表达式 re.match()

    import re
    def splitFunc( ):
        tmpStr = input('请输入字符串:')
        charNum = 0
        digNum = 0
        spaceNum = 0
        otherNum = 0
        for i in range(len(tmpStr)):
            if re.match('[a-zA-Z]',tmpStr[i]):
                charNum +=1
            elif re.match('d',tmpStr[i]):
                digNum +=1
            elif re.match('s',tmpStr[i]):
                spaceNum +=1
            else:
                otherNum +=1
        print('字符:',charNum)
        print('数字:',digNum)
        print('空格:',spaceNum)
        print('其他:',otherNum)
    splitFunc() 
    

      

  • 相关阅读:
    linux 常用命令大全
    socket的读写函数readn和writen函数
    python中exec 和eval的用法
    python中set集合介绍
    python中下划线变量的规则和意义
    关于小端字节序和大端字节序的解释
    需要学习的网站
    关于尾递归节省内存空间
    python中的with语句
    python中的类变量和对象变量,以及传值传引用的探究
  • 原文地址:https://www.cnblogs.com/python-xkj/p/9225985.html
Copyright © 2011-2022 走看看