输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。
数据范围:输入的字符串长度满足
""" from numpy import s_ 统计各个字符的数量,有两种方法 1. 正则匹配 2. 使用count """ s = input() # 找出元 s_sole = list(set(list(s))) letter_count = 0 space_count = 0 num_count = 0 other_count = 0 for letter in s: if letter.isalpha(): letter_count += 1 elif letter.isdigit(): num_count += 1 elif letter == ' ': space_count += 1 else: other_count += 1 print(letter_count) print(space_count) print(num_count) print(other_count)
s = input() nalph, nspace, ndigit, nother = 0, 0, 0, 0 for i in s: if i.isalpha(): nalph += 1 elif i == ' ': nspace += 1 elif i.isdigit(): ndigit += 1 else: nother += 1 print(nalph) print(nspace) print(ndigit) print(nother)
st = input() c1 = 0 c2 = 0 c3 = 0 c4 = 0 for i in st: if i.isalpha(): c1 += 1 elif i == ' ': c2 += 1 elif i.isnumeric(): c3 += 1 else: c4 += 1 print(c1) print(c2) print(c3) print(c4)
upper_alphabet = [chr(i) for i in range(ord('A'),ord('Z')+1)] lower_alphabet = [chr(i) for i in range(ord('a'),ord('z')+1)] numbers = [str(i) for i in range(10)] input_str = input() alphabet_num = 0 space_num = 0 number_num = 0 others_num = 0 for c in input_str: if c in (upper_alphabet + lower_alphabet): alphabet_num += 1 elif c == ' ': space_num += 1 elif c in numbers: number_num += 1 else: others_num += 1 print(alphabet_num) print(space_num) print(number_num) print(others_num)
string = input() count_alpha = 0 count_blank = 0 count_digit = 0 count_other = 0 for code in string: if code.isalpha(): count_alpha += 1 elif code == ' ': count_blank += 1 elif code.isdigit(): count_digit += 1 else: count_other += 1 print(count_alpha) print(count_blank) print(count_digit) print(count_other)
import re a = input() if len(a) in range(1, 1001): cou_num = [0, 0, 0, 0] cou_num[0] = len(re.findall(r"[a-zA-Z]", a)) cou_num[1] = len(re.findall(r"[\s]", a)) cou_num[2] = len(re.findall(r"[\d]", a)) cou_num[3] = len(a) - sum(cou_num[0:3]) for i in cou_num: print(i)
str = input() zy = 0 kg = 0 num =0 el = 0 for i in str: if i == ' ': kg += 1 elif '0' <= i <= '9': num += 1 elif 65 <= ord(i) <=90&nbs***bsp;97 <= ord(i) <= 122: zy += 1 else: el += 1 print(f"{zy}\n{kg}\n{num}\n{el}\n")