Linux 拨号vps windows公众号手机端

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

lewis 7年前 (2018-03-20) 阅读数 6 #程序编程
文章标签 python

可以使用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.
版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

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

热门