阅读背景:

6.11作业6:写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数

来源:互联网 
'''2、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数'''
# 13 回车
# 32 空格
# 48-57 数字是字符串的
# 65-90 A-Z
# 97-122 a-z
# ord(a)a转为acsii
# chr(b)b对应的字符为


print('first'.center(40,'='))
def fun1(str1):
    dig = 0
    al = 0
    tb = 0
    other = 0
    for i in str1:
        i = ord(i)
        if i >= 48 and i <=57:
            dig += 1
        elif (i >= 65 and i <= 90) or (i >= 97 and i <= 122):
            al += 1
        elif i == 32:
            tb += 1
        else:
            other += 1
    print('字符串中【数字】{}个、【字母】{}个、【空格】{}个 、 【其他】{}个'.format(dig,al,tb,other))
str2 = input("Please enter the first string:")
fun1(str2)


print('second'.center(40,'='))
import re
def fun2(str11):
    dig = 0
    al = 0
    tb = 0
    other = 0
    for i in str11:
        if re.match('[\d]',i) != None:
            dig += 1
        elif re.match('[A-Za-z]',i) != None:
            al += 1
        elif re.match('\s',i) != None:
            tb += 1
        else:
            other += 1
    print('字符串中【数字】{}个、【字母】{}个、【空格】{}个 、 【其他】{}个'.format(dig, al, tb, other))
str22 = input("Please enter the second string:")
fun2(str22)
'''2、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数'''
#



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: