可以使用Python中的Counter类来统计不同字符的个数。下面是一个例子:
from collections import Counter
def count_characters(text):
counter = Counter(text)
for char, count in counter.items():
print(f"Character '{char}' appears {count} times")
text = "Hello, World!"
count_characters(text)
这段代码会输出每个字符及其出现的次数。你可以替换text
变量的值来统计不同字符的个数。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: python爬虫正则匹配怎样处理空白