
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively
When it is required to check the number of times a given letter occurs in a string using recursion, a method can be defined, and an ‘if’ condition can be used.
The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.
Example
Below is a demonstration for the same −
def check_frequency(my_str,my_ch): if not my_str: return 0 elif my_str[0]==my_ch: return 1+check_frequency(my_str[1:],my_ch) else: return check_frequency(my_str[1:],my_ch) my_string = input("Enter the string :") my_char = input("Enter the character that needs to be checked :") print("The frequency of " + str(my_char) + " is :") print(check_frequency(my_string,my_char))
Output
Enter the string :jaanea Enter the character that needs to be checked :a The frequency of a is : 3
Explanation
- A method named ‘check_frequency’ is defined that takes a string and a character as parameters.
- It checks to see if the characters in a string match the character passed to the method.
- If they do, it is returned.
- Else the method is called recursively on all characters of the string.
- The string and the character are taken as user inputs.
- The method is called by passing these values as parameters.
- The output is dislayed on the console.
- Related Articles
- Python Program to Determine Whether a Given Number is Even or Odd Recursively
- Golang Program to Determine Recursively Whether a Given Number is Even or Odd
- Program to count how many times we can find "pizza" with given string characters in Python
- Program to determine the minimum cost to build a given string in python
- Program to find how many times a character appears in a string in PHP
- Finding how many times a specific letter is appearing in a sentence in JavaScript
- Check if a string can become empty by recursively deleting a given sub-string in Python
- Python Program to Search the Number of Times a Particular Number Occurs in a List
- C++ program to concatenate a string given number of times?
- How to check if a character in a string is a letter in Python?
- Program to find a good string from a given string in Python
- Program to find out the letter at a particular index in a synthesized string in python
- Python program to Count words in a given string?
- Program to count k length substring that occurs more than once in the given string in Python
- Python Program to find out how many times the balls will collide in a circular tube

Advertisements