Python Training 1

#统计一个字符串中数字、字母、其他类型字符的个数

s=input('Please input you string:')
print(s,'is all consisted of alpha?:',s.isalpha())#isalpha function
print(s,'is all consisted of digit?:',s.isdigit())#isdigit function

alphacount=0 #the original sum of alpha
digitcount=0 #the original sum of digit
othercount=0 #the original sum of others

for i in s:
    if i.isalpha()==True:
        alphacount=alphacount+1
    elif i.isdigit()==True:
        digitcount=digitcount+1
    else:
        othercount=othercount+1

print('The total sum of alpha:',alphacount)
print('The total sum of digit:',digitcount)
print('The total sum of others:',othercount)
    
输出举例:
Please inout you string:123456,,,...@@@$$$Inspiration
'123456,,,...@@@$$$Inspiration' is all consisted of alpha?: False
'123456,,,...@@@$$$Inspiration' is all consisted of digit?: False
The total sum of alpha: 11
The total sum of digit: 6
The total sum of others: 12

你可能感兴趣的:(python,string,函数,Python)