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
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.
Similar Questions
Write a Python program to count the number of occurrences of a specific character in a string
Define the function count_substring(string, substring) that finds how many times the substring appears in the given string.Example:string = "Python is a powerful language. I want to learn Python"substring = "Python"Expected Output:2
In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left.NOTE: String letters are case-sensitive.Input FormatThe first line of input contains the original string. The next line contains the substring.ConstraintsEach character in the string is an ascii character.Output FormatOutput the integer number indicating the total number of occurrences of the substring in the original string.Sample InputABCDCDCCDCSample Output2ConceptSome string processing examples, such as these, might be useful.There are a couple of new concepts:In Python, the length of a string is found by the function len(s), where is the string.To traverse through the length of a string, use a for loop:for i in range(0, len(s)): print (s[i])A range function is used to loop over some length:range (0, 5)Here, the range loops over to . is excluded.
Count the sentences, words and characters in a fileWrite a Python program that reads a file and prints the number of lines, words, and characters in the file.Input Format:The input will be a single string, which is the name of the file to be read.Output Format:The output will be three separate lines, each containing a count:The first line will contain the number of lines in the file, prefixed with "Sentences: ".The second line will contain the number of words in the file, prefixed with "Words: ".The third line will contain the number of characters in the file, prefixed with "Chars: ".Sample Test CasesTest Case 1:Expected Output:file·name:input1.txtSentences:·8Words:·34Chars:·228Test Case 2:Expected Output:file·name:input2.txtSentences:·7Words:·51Chars:·306
Write a program to find the substring of a given string.
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.