## Thursday, October 22, 2020

Solution Exercise 57: python program that find the number of occurrence within a given string #### Exercise 57

Write a python program as a function which takes as parameter a tuple of string (s, s1) and which returns the number of the occurrences of s1 within the string s. Example if s = "Python is object oriented and dynamically typed " and s1 = "ed" , the function returns 2

#### Solution

``````def numberOccurrences(s , s1):
n = len(s)
m = len(s1)

# initializing the number of occurrences of s1 wthin the string s.
occurrences = 0

for i in range(0 , n-m):
if s[i:i+m] == s1:
occurrences = occurrences + 1
return occurrences

s = "Python is object oriented and dynamically typed "
s1 = "ed"
print(numberOccurrences(s , s1)) # display 2``````

Younes Derfoufi
my-courses.net

## Python Exercises With Solutions

1. Python Arithmetic Exercises
2. Equations & System Of Equations
3. Polynomials and Functions
4. Python Arithmetic Exercises
5. Numpy, Matplolib, Sympy , Scipy - Exercises
6. Python Arithmetic Exercises

Younes Derfoufi

## Sample Text 