Open In App

Recursively Count Vowels From a String in Python

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Python is a versatile and powerful programming language that provides various methods to manipulate strings. Counting the number of vowels in a string is a common task in text processing. In this article, we will explore how to count vowels from a string in Python using a recursive method. Recursion is a programming concept where a function calls itself in its definition, allowing for concise and elegant solutions to certain problems.

What is Recursive Method in Python For Count Vowels?

The main idea behind the recursive approach to counting vowels is to break down the problem into smaller subproblems. In this case, we can break the string into smaller parts and recursively count the vowels in each substring. The base case is when the string becomes empty, at which point we return 0. Otherwise, we check if the first character is a vowel and add 1 to the count if true. We then recursively call the function with the remaining substring.

Syntax :

In the below Syntax :

  • s[0] accesses the first character of the string.
  • s[1:] represents the substring starting from the second character to the end of the string.
  • .lower() ensures case-insensitivity, considering both uppercase and lowercase vowels

Python3




def count_vowels_recursive(s):
    # Base case: empty string
    if not s:
        return 0
    # Check if the first character is a vowel
    elif s[0].lower() in 'aeiou':
        return 1 + count_vowels_recursive(s[1:])
    else:
        return count_vowels_recursive(s[1:])


Count Vowels From A String In Python Using Recursive Method

Below, are the examples of Count Vowels From A String In Python Using Recursive Method.

Example 1:

  • The input string is “Hello World.”
  • The count_vowels_recursive function is called to count vowels recursively, resulting in a count of 3 vowels (e, o, o).

Python3




input_string = "Hello World"
 
result = count_vowels_recursive(input_string)
print(f"The number of vowels in '{input_string}' is: {result}")


Output

The number of vowels in 'Recursive Methods' is: 6

Example 2:

  • The input string is “Recursive Methods.”
  • The count_vowels_recursive function is invoked, and it counts 6 vowels (e, u, i, e, e, o) in a recursive manner.

Python3




input_string = "Recursive Methods"
 
result = count_vowels_recursive(input_string)
print(f"The number of vowels in '{input_string}' is: {result}")


Output

The number of vowels in 'Recursive Methods' is: 6

Example 3:

  • The input string is “Python Programming.”
  • The count_vowels_recursive function is used to recursively count 4 vowels (o, o, a, i) in the given string.

Python3




input_string = "Python Programming"
 
result = count_vowels_recursive(input_string)
print(f"The number of vowels in '{input_string}' is: {result}")


Output

The number of vowels in 'Python Programming' is: 4

Conclusion

In this article, we explored a recursive method to count vowels from a string in Python. The recursive approach breaks down the problem into smaller subproblems, making the code concise and readable. By understanding the base case and recursive calls, we can efficiently count the number of vowels in a given string. Consider using this approach when dealing with similar text processing tasks that can benefit from recursion.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads