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
精彩评论