python 统计字符串中某个字符出现次数
要统计字符串中某个字符出现的次数,可以使用count()方法。这个方法接受一个字符作为参数,并返回该字符在字符串中出现的次数。
以下是一个例子:
string = "Hello, World!"
char = 'o'
count = string.count(char)
print(f"The character '{char}' appears {count} times in the string.")
输出结果为:
The character 'o' appears 2 times in the string.
原文地址: https://www.cveoy.top/t/topic/iJpG 著作权归作者所有。请勿转载和采集!