Q:

Write a program to find how many times substring “Emma” appears in the given string.

belongs to collection: Python Basic Exercises for Beginners

0

Return the count of a given substring from a string

Write a program to find how many times substring “Emma” appears in the given string.

Given:

str_x = "Emma is good developer. Emma is a writer"

Expected Output:

Emma appeared 2 times

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

Use string method count().

Solution 1: Use the count() method

str_x = "Emma is good developer. Emma is a writer"
# use count method of a str class
cnt = str_x.count("Emma")
print(cnt)

Solution 2:Without string method

def count_emma(statement):
    print("Given String: ", statement)
    count = 0
    for i in range(len(statement) - 1):
        count += statement[i: i + 4] == 'Emma'
    return count

count = count_emma("Emma is good developer. Emma is a writer")
print("Emma appeared ", count, "times")

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Print the following pattern using Python programmi... >>
<< Display numbers divisible by 5 from a list...