方法一:
num = 0
char = 0
other = 0
s = input('请输入一个字符串:')
for i in range(len(s)):
if ( '0' <= s[i] <= '9'):
num +=1
continue
if (('a' <= s[i] <= 'z') or ('A' <= s[i] <= 'Z')):
char +=1
continue
else:
other +=1
print('字母有:%d个,数字有:%d个,其它符号有:%d个' % (char,num,other))
方法二:
num = 0
char = 0
other = 0
s = input('请输入一个字符串:')
for i in range(len(s)):
if ( s[i].isdigit()):
num +=1
continue
if ((s[i].isalpha())):
char +=1
continue
else:
other +=1
print('字母有:%d个,数字有:%d个,其它符号有:%d个' % (char,num,other))
本文介绍了两种Python方法,分别通过遍历字符串并使用内置函数isalpha()和isdigit()来计算其中字母、数字和其它字符的数量。
2263

被折叠的 条评论
为什么被折叠?



