dvanaria
dvanaria

Reputation: 6783

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.

Upvotes: 0

Views: 880

Answers (2)

kurumi
kurumi

Reputation: 25599

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

Upvotes: 3

rlibby
rlibby

Reputation: 6021

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

Refer to the documentation for strings.

Upvotes: 7

Related Questions