开发者

Python: count occurances of a given char in a string

How would I开发者_Python百科 take a string in Python like "/bin/usr/proga/file.c" and count the occurrences of the '/' character?

So, for the above example, the function would return 4.


"/bin/usr/proga/file.c".count("/")

Refer to the documentation for strings.


>>> s="/bin/usr/proga/file.c"
>>> s.count("/")
4
>>> len(s.split("/"))-1
4
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜