700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Python判断字符类型

Python判断字符类型

时间:2023-01-04 12:03:14

相关推荐

Python判断字符类型

题目描述:

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

实现代码:

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

运行演示:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。