开发者

Python: count occurances of a given char in a string

开发者 https://www.devze.com 2023-02-25 00:35 出处:网络
How would I开发者_Python百科 take a string in Python like \"/bin/usr/proga/file.c\" and count the occurrences of the \'/\' character?

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

精彩评论

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

关注公众号