输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
alph=[]
digit=[]
space=[]
other=[]
str_1=str(input("请输入你要统计的字符串:"))
for i in range(len(str_1)):
if str_1[i].isalpha():
alph.append(str_1[i])
if str_1[i].isdigit():
digit.append(str_1[i])
if str_1[i].isspace():
space.append(str_1[i])
if not str_1[i].isalnum() and not str_1[i].isspace():
other.append(str_1[i])
print("输入的字符串中字符有",alph,'共有',len(alph),'个。')
print('输入的字符串中数字有',digit,'共有',len(digit),'个。')
print('输入的字符串中空格有',space,'共有',len(space),'个。')
print('输入的字符串中其他字符有',other,'共有',len(other),'个。')
主要熟悉对字符串进行操作的相关函数。