StudyGPT Just Leveled Up – Say Hello to Knowee! 🚀
Knowee
Questions
Features
Study Tools

Write a python program to print the number of occurrences of a substring into a line

Question

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Python program that counts the number of occurrences of a substring in a string:

def count_substring(string, substring):
    return string.count(substring)

string = "Hello, world! This is a world full of surprises."
substring = "world"

print("The substring occurs", count_substring(string, substring), "times.")

In this program, we define a function count_substring that takes two arguments: the main string and the substring we want to count. The function uses the count method of the string to count the number of occurrences of the substring.

We then define a string and a substring, and print the number of occurrences of the substring in the string by calling our function.

This problem has been solved

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.