Python - Count The Number of Substrings

preview_player
Показать описание
Welcome to a new Coding Video!!!
----------------------------------------------------------------------------------------------------------------------------

If you like my content, please SUBSCRIBE, Hit the notification bell, and SHARE.
Thank you for your support!!
----------------------------------------------------------------------------------------------------------------------------

For private request, paid basic desktop applications, or just motivation, don't hesitate to contact me at:
----------------------------------------------------------------------------------------------------------------------------

Explanation

Ex: main_string = 'abcdjhebobsdbob'
sub_string = 'bob'
The output here should be 2 since we find 'bob' two times in the main string.

One way to do this is to just iterate over the main string, and at each time we find the
substring, we increment the counter by 1.

This works because at each index, we are checking if the string of lenght second string
is equal to the substring we are looking for.

Ex: 'absfdbobdfdbob'
'bob'
If we are at index 0, we check the string starting at 0 and ending at 0 + len('bob')
Here this string is 'abs'
At each index we do the same and compare it with the substring we are searching for
and increment the counter accordingly.

----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------

Tags
#Python #Fun #Substrings
Рекомендации по теме