700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 利用函数计算传入字符串中的数字字母空格和其他的个数 – python – 前端 python自带数据库

利用函数计算传入字符串中的数字字母空格和其他的个数 – python – 前端 python自带数据库

时间:2018-11-11 21:06:55

相关推荐

利用函数计算传入字符串中的数字字母空格和其他的个数 – python – 前端 python自带数据库

defcount_str(strs):int_count,str_count,spa_count,other_count = 0,0,0,0

for i in strs:# 遍历字符串

if i.isdigit(): # 判断是否为数字

int_count += 1

elif i.isalnum(): # 判断是否为字母

str_count += 1

elif i.isspace(): # 判断是否为空格

spa_count += 1

else:

other_count +=1

print(“字符串s中,数字个数={},字母个数={},空格个数={},其他个数={}”.format(int_count,str_count,spa_count,other_count))

if __name__ == “__main__”:

strs = input(“请输入字符串s:”)

count_str(strs)

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