栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python判断字符类型

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python判断字符类型

题目描述:

输入一行字符,输出其中空格、数字、中文字符、英文字符和其他字符的个数。

实现代码:
str=input("请输入一行字符:n")
chinese=0
letters=0
space=0
digit=0
others=0
for c in str:
    if  c.isspace():
        space+=1
    elif c.isdigit():
        digit+=1
    elif c>=u'u4e00' and c<=u'u9fa5':#判断是否为中文字符
        chinese+=1
    elif c.isalpha():
        letters += 1
    else:
        others+=1
print('空格=%d,数字=%d,中文字符=%d,英文字符=%d,其他字符=%d'%(space,digit,chinese,letters,others))

运行演示:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/833746.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号