i=input("请输入任意一段字符串: ")
chi,num,y,Y,other,spaces=0,0,0,0,0,0
spaces=i.count(" ")
i=i.replace(" ","") #删除字符串中空格,以防后面出现其他字符扰乱
j=list(i) #将字符串列表化,进行for循环统计元素
for n in j:
if 19968<=ord(n)<=40869: #利用Unicode编码将各个元素分离
chi+=1
elif 48<=ord(n)<=57:
num+=1
elif 97<=ord(n)<=122:
y+=1
elif 65<=ord(n)<=90:
Y+=1
else:
other+=1
print("该段字符串中:")
print("有空格:{:9}个".format(spaces))
print("有汉字:{:9}个".format(chi))
print("有数字:{:9}个".format(num))
print("有小写字母:{:5}个".format(y))
print("有大写字母:{:5}个".format(Y))
print("有其他字符:{:5}个".format(other))
统计输入一段字符串的各个元素
最新推荐文章于 2022-12-03 03:21:25 发布