Skip to content Skip to sidebar Skip to footer

Python: Count Occurances Of A Given Char In A String

How would I 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.

Solution 1:

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

Refer to the documentation for strings.


Solution 2:

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

Post a Comment for "Python: Count Occurances Of A Given Char In A String"