How to count the number of occurrences of a substring in a larger string in python
python
Software and digital electronics / Coding
2023-01-04 10:03
Setup
I have a python string like
superstring = '/aaa/bbb/ref/ccc/ddd/ref/eee/fff/' substring1 = '/' substring2 = '/ref/'
I would like to count the number of occurrences of substring1 and substring2 inside the superstring.
How can I achieve that?
add comment
Answered by ernest1
2023-01-04 10:21
Use .count() function for this purpose. The commands
superstring.count('/')
superstring.count(substring2)
will return
9
2
add comment