Python编写一个函数计算传入的字符串数字、字母、空格及其他字符的个数

#编写一个函数计算传入字符串中数字、字母、空格以及其它字符的个数

*******************************************************************************************
def count(s):
  alpha,num,space,other=0,0,0,0
  for i in s:
    if i.isalpha():
        alpha+=1
    elif i.isdigit():
        num+=1
    elif i.isspace():
        space+=1
    else:
        other+=1
  print('英文字符数{},数字字符数{},空格字符数{},其他字符数{}'.format(alpha,num,space,other))
count(input("请输入一个字符串:"))
*******************************************************************************************

运行结果:

你可能感兴趣的:(Python应用)