怎样用Python分别统计各种字符的个数?

时间:2026-02-15 20:19:24

1、首先,右键点击【项目】,创建一个【Python】文件

怎样用Python分别统计各种字符的个数?

2、接着,在右侧输入代码,代码写在【下一步】

怎样用Python分别统计各种字符的个数?

3、然后,复制代码粘贴到文件中

import string


s = '123runoobc  kdf235*(dfl'
letters = 0
space = 0
digit = 0
others = 0
i=0
while i < len(s):
   c = s[i]
   i += 1
   if c.isalpha():
       letters += 1
   elif c.isspace():
       space += 1
   elif c.isdigit():
       digit += 1
   else:
       others += 1
print ('char = %d,space = %d,digit = %d,others = %d' % (letters,space,digit,others))

怎样用Python分别统计各种字符的个数?

4、随后,右键选择运行代码文件

怎样用Python分别统计各种字符的个数?

5、最后,在最下面就可以看到运行结果了

怎样用Python分别统计各种字符的个数?

© 2026 一点资料
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com