python 编程功能:输入自定义的字符串,利用函数计算传入字符串中的数字、字母、空格和其他的个数? Python利用自定义函数,任意输入字符串,去掉所有的空格(...

\u7528python\u5199\u7a0b\u5e8f\u5b9e\u73b0\uff1a\u8f93\u5165\u4e00\u5b57\u7b26\u4e32\uff0c\u5206\u522b\u7edf\u8ba1\u5176\u4e2d\u7684\u82f1\u6587\u5b57\u6bcd\u4e2a\u6570\uff0c\u7a7a\u683c\u3001\u6570\u5b57\u548c\u5176\u4ed6\u5b57\u7b26\u3002

wz="\u8ba1\u91cf\u5355\u4f4d\u662f\u6307\u6839\u636e\u7ea6\u5b9a\u5b9a\u4e49\u548c\u91c7\u7528\u7684\u6807\u91cf\uff0c\u4efb\u4f55\u5176\u4ed6\u540c\u7c7b\u91cf\u53ef\u4e0e\u5176\u6bd4\u8f83\u4f7f\u4e24\u4e2a\u91cf\u4e4b\u6bd4\u7528\u4e00\u4e2a\u6570\u8868\u793a\u3002\u8ba1\u91cf\u5355\u4f4d\u5177\u6709\u6839\u636e\u7ea6\u5b9a\u8d4b\u4e88\u7684\u540d\u79f0\u548c\u7b26\u53f7\u3002"
for i in wz:
print("%s\u51fa\u73b0:%d\u6b21"%(i,wz.count(i)))

def out_space(s):
a = ''
for i in s:
if i != ' ':
a += i
return a

str_1 = input("请输入一个字符串:")
num_num = 0
num_alp = 0
num_null = 0
num_else = 0
for s in str_1:
if s >= '0' and s <= '9':
num_num += 1
elif (s >= 'a' and s <= 'z') or (s >= 'A' and s <= 'Z'):
num_alp += 1
elif s == ' ':
num_null += 1
else:
num_else += 1
print("数字的个数:",num_num)
print("字母的个数:",num_alp)
print("空格的个数:",num_null)
print("其他的个数:",num_else)

运行结果:
请输入一个字符串:haha 123 ** 11
数字的个数: 5
字母的个数: 4
空格的个数: 3
其他的个数: 2

望采纳!

扩展阅读:python手机版下载安装 ... python编程入门自学 ... 少儿python编程自学 ... python在线编程入口 ... python初学编程必背 ... python接单一般价格 ... 免费的python编程软件 ... python网站入口免费 ... python编程必背单词 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网