本文主要是实现:输入字符串,计算它的字母、数字、空格和其他符号的个数
代码:
#!/usr/bin/env python# -*- coding:utf-8 -*-# author by lh r=raw_input("请输入您要计算的字符串:") def func(s): all_alphanum=0 #初始化字母的总数为0 all_othersnum=0 #初始化其他字符总数为0 all_spacenum=0 #初始化空格的总数为0 all_digitnum=0 #初始化数字的总数为0 for i in s: if i.isdigit(): all_digitnum+=1 elif i.isspace(): all_spacenum+=1 elif i.isalpha(): all_alphanum+=1 else: all_othersnum+=1 return(all_digitnum,all_spacenum,all_alphanum,all_othersnum) ret=func(r)print ret
运行结果:
时间: 2024-10-22 19:31:25