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("/")
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"