输入一行字符,判断不同字符的数量, 分别用for循环和while循环完成
for循环
运用了字符串方法,
isupper()判断是否为大写字母
islower()判断是否为小写字母
isdigit()判断是否为数字
1 n = input("输入一行字符:")
2 daxie = 0
3 xiaoxie = 0
4 num = 0
5 other = 0
6 for data in n:
7 if data.isupper():
8 daxie += 1
9 elif data.islower():
10 xiaoxie += 1
11 elif data.isdigit():
12 num += 1
13 else:
14 other += 1
15 print("大写字母:%s个 小写字母:%s个 数字:%s个 其他字符:%s个" %(daxie,xiaoxie,num,other))
while循环
判断部分用的字符串切片
1 n = input("输入一行字符:")
2
3 daxie = 0
4 xiaoxie = 0
5 num = 0
6 other = 0
7 i = 1
8 while i <= len(n):
9 if (n[:i])[i-1:].isupper():
10 daxie += 1
11 elif (n[:i])[i-1:].islower():
12 xiaoxie += 1
13 elif (n[:i])[i-1:].isdigit():
14 num += 1
15 else:
16 other += 1
17 i += 1
18 print("大写字母:%s个 小写字母:%s个 数字:%s个 其他字符:%s个" %(daxie,xiaoxie,num,other))
转载于:https://www.cnblogs.com/Handsome-Lan/p/10731806.html