Knowee
Questions
Features
Study Tools

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

Question

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

🧐 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

Similar Questions

Write a Python program to count the number of occurrences of a specific character in a string

Write a python program to count the number of palindrome strings that occurred in the given string.

Write a python program to count the number of characters of alphabets 'O' and 'i' from the given file

Write a python program that traverses through an input string and prints its characters indifferent lines – two characters per line

Python program that accepts a string and calculate the number of digits and letters and space

1/2

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.