Python中怎么计算字符串中特定字符出现的次数

lewis 2018-03-20 21次阅读

可以使用count()方法来计算字符串中特定字符出现的次数。例如:

string = "Hello, World!"
char = "l"

count = string.count(char)
print(f"The character '{char}' appears {count} times in the string.")

这将输出:

The character 'l' appears 3 times in the string.


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。