计算字符串中某个字符出现的次数 [英] Count the number of occurrences of a character in a string

查看:53
本文介绍了计算字符串中某个字符出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计算字符串中某个字符出现次数的最简单方法是什么?

例如计算'a''Mary had a little lamb'中出现的次数.

解决方案

str.count(sub[, start[, end]])

返回[start, end]范围内子串sub的非重叠出现次数.可选参数 startend 被解释为切片符号.

<预><代码>>>>句子 = '玛丽有一只小羊羔'>>>句子.count('a')4

What's the simplest way to count the number of occurrences of a character in a string?

e.g. count the number of times that 'a' appears in 'Mary had a little lamb'.

解决方案

str.count(sub[, start[, end]])

Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.

>>> sentence = 'Mary had a little lamb'
>>> sentence.count('a')
4

这篇关于计算字符串中某个字符出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆