在一行上输入一个长度为
的字符串。
第一行输出一个整数,代表字符串中英文字母的个数。
第二行输出一个整数,代表字符串中空格的个数。
第三行输出一个整数,代表字符串中数字的个数。
第四行输出一个整数,代表字符串中其它字符的个数。
1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][
26 3 10 12
s = input() a,b,c,d = (0,0,0,0) for i in s: if i.isalpha() : a += 1 elif i ==' ': b += 1 elif i.isdigit(): c += 1 else: d += 1 print(a,b,c,d,sep='\n')
# 解法,使用ascall判断英文字母,注意小写字母的a是97-122,大写字母是65-90;所以在90到97之间是特殊符号,注意区分 # def static(str1): try: j=0 # 空格 k=0 # 数字 z=0 # 字母 w=0 # 特殊符号 for i in str1: # 先统计空格 if i == " ": j+=1 # 统计数字 elif ord(i) in range(ord("0"),ord("9")+1) : k+=1 # 判断是否为英文字符 elif ord(i) in range(ord("A"),ord("Z")+1): z+=1 # print(i) elif ord(i) in range(ord("a"),ord("z")+1): z += 1 # print(i) else: w+=1 print(z,j,k,w,sep='\n') except Exception as e: print(e) if __name__ == "__main__": str1 = input() static(str1)
import sys for line in sys.stdin: a = line result = {} for i in a: if(i.isalpha()): if("alpha" not in result.keys()): result["alpha"] = 1 else: result["alpha"] = result["alpha"]+1 elif(i.isdigit()): if("digit" not in result.keys()): result["digit"] = 1 else: result["digit"] = result["digit"]+1 elif(i == " "): if("blank" not in result.keys()): result["blank"] = 1 else: result["blank"] = result["blank"]+1 else: if("other" not in result.keys()): result["other"] = 1 else: result["other"] = result["other"]+1 if("alpha" in result.keys()): print(result["alpha"]) else: print(0) if("blank" in result.keys()): print(result["blank"]) else: print(0) if("digit" in result.keys()): print(result["digit"]) else: print(0) if("other" in result.keys()): print(result["other"]-1) else: print(0)我这个代码最后没办法必须要将other强行减一,我发现other总会多一个。是不是因为字符串里面最后还包含一个换行符?
s = input() #英文字符: def alpha(s): for i in s: if not i.isalpha(): s = s.replace(i,'') lenalpha = len(s) return lenalpha print(alpha(s)) #空格数: lenwhole = len(s) lenspace = lenwhole-len(s.replace(' ','')) print(lenspace) #数字字符: def digit(s): for i in s: if not i.isdigit(): s = s.replace(i,'') lendigit = len(s) return lendigit print(digit(s)) #其他字符数: def other(s): for i in s: if i.isdigit()&nbs***bsp;i.isalpha(): s = s.replace(i,'') s1 = s.replace(' ','') lenother = len(s1) return lenother print(other(s))
s = input() ls = [0]*4 for i in s: if i.lower().islower(): ls[0] += 1 elif i.isspace(): ls[1] += 1 elif i.isdigit(): ls[2] += 1 else: ls[-1] += 1 for a in ls: print(a)
nums = '0123456789' abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' s = input() a = 0 b = 0 c = 0 d = 0 for i in range(len(s)): if s[i] in abc: a =a+1 elif s[i] == ' ': b = b+1 elif s[i] in nums: c = c+1 else: d = d +1 print(a) print(b) print(c) print(d)
ip = input() spaceNum = ip.count(' ') ip = ip.replace(' ','') charNum = 0 numNum = 0 otherNum = 0 for char in ip: if ord('a')<=ord(char)<=ord('z')&nbs***bsp;ord('A')<=ord(char)<=ord('Z'): charNum += 1 elif char in "0123456789": numNum += 1 else: otherNum += 1 print(charNum) print(spaceNum) print(numNum) print(otherNum)
s = input() res = [0, 0, 0, 0] for i in s: if i.isalpha(): res[0] += 1 elif i == ' ': res[1] +=1 elif i.isdigit(): res[2] += 1 else: res[3] += 1 for n in res: print(n)
from collections import Counter while True: try: words = input() words = Counter(words) count_alpha, count_space, count_digit, count_other = 0,0,0,0 for word in words: if word.isalpha(): count_alpha += words[word] elif word.isspace(): count_space += words[word] elif word.isdigit(): count_digit += words[word] else: count_other += words[word] print(count_alpha) print(count_space) print(count_digit) print(count_other) except: break
inp = input().upper() word_list = [chr(i) for i in range(ord('A'), ord('Z')+1)] num_list = [str(i) for i in range(10)] word, num = 0, 0 space = inp.count(' ') for i in inp: if i in word_list: word += 1 elif i in num_list: num += 1 else: continue print(str(word) + '\n' + str(space) + '\n' + str(num) + '\n' + str(len(inp)-word-num-space))
n =input() a,b,c,d = 0,0,0,0 for i in n: if i.isalpha(): a +=1 elif i.isspace(): b +=1 elif i.isalnum(): c +=1 else: d +=1 print(a) print(b) print(c) print(d)考察类型判断