python练习17:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

python练习17:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。,第1张

#输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

#思路:找到目标(正则),计数()

from typing import Counter

import re

input='ASDgfrf   %%%%@#'

#找到所有英文字母--计算数量

str_list=re.findall('[a-zA-z]',input)#!!!!findall:配 返回为列表。

print('number of string is %d'%len(str_list))#!!!!len():返回容器(eg列表 字符串)的对象个数,

#找到所有空格--计算数量

space_list=re.findall(' ',input)

print('number of space is %d'%len(space_list))

#找到所有其他字符--计算数量

other_list=re.findall('[^a-zA-z ]',input)

print('number of other string is %d'%len(other_list))

#输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
#思路:找到目标(正则),计数()
from typing import Counter
import re
input='ASDgfrf   %%%%@#'
#找到所有英文字母--计算数量
str_list=re.findall('[a-zA-z]',input)#!!!!findall:配 返回为列表。
print('number of string is %d'%len(str_list))#!!!!len():返回容器(eg列表 字符串)的对象个数,
#找到所有空格--计算数量
space_list=re.findall(' ',input)
print('number of space is %d'%len(space_list))
#找到所有其他字符--计算数量
other_list=re.findall('[^a-zA-z ]',input)
print('number of other string is %d'%len(other_list))

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/717257.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存