Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Replace vowels in a string with a specific character K
Next article icon

Recursively Count Vowels From a String in Python

Last Updated : 14 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
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.


Next Article
Python - Replace vowels in a string with a specific character K
author
dukuru_venkatesh
Improve
Article Tags :
  • Python
  • Python Programs
  • python-string
Practice Tags :
  • python

Similar Reads

  • How to Count Repeated Words in a String in Python
    In this article, we will learn how to count repeated words in a string. Python provides several methods to Count Repeated Words , such as dictionaries, collections. Counter module, or even regular expressions. The simplest way to count repeated words is by splitting the string into individual words
    2 min read
  • Python - Split String on vowels
    Given a String, perform split on vowels. Example: Input : test_str = 'GFGaBst' Output : ['GFG', 'Bst'] Explanation : a is vowel and split happens on that. Input : test_str = 'GFGaBstuforigeeks' Output : ['GFG', 'Bst', 'f', 'r', 'g', 'ks']Explanation : a, e, o, u, i are vowels and split happens on th
    5 min read
  • Python - Remove all consonants from string
    Sometimes, while working with Python, we can have a problem in which we wish to remove all the non vowels from strings. This is quite popular question and solution to it is useful in competitive programming and day-day programming. Lets discuss certain ways in which this task can be performed.Method
    7 min read
  • Python | Case Counter in String
    Sometimes, while working with Python String, we can have a problem in which we need to separate the lower and upper case count. This kind of operation can have its application in many domains. Let's discuss certain ways in which this task can be done. Method #1: Using map() + sum() + isupper() + isl
    7 min read
  • Python - Replace vowels in a string with a specific character K
    The task is to replace all the vowels (a, e, i, o, u) in a given string with the character 'K'. A vowel can be in uppercase or lowercase, so it's essential to account for both cases when replacing characters. Using str.replace() in a LoopUsing str.replace() in a loop allows us to iteratively replace
    3 min read
  • Python - Possible Substring count from String
    Given target string and argument substring, count how many substrings can be constructed using string characters, repetitions not allowed. Input : test_str = "geksefokesgergeeks", arg_str = "geeks" Output : 3 Explanation : "geeks" can be created 3 times using string characters. Input : test_str = "g
    4 min read
  • Python | Strings with similar front and rear character
    Sometimes, while programming, we can have a problem in which we need to check for the front and rear characters of each string. We may require to extract the count of all strings with similar front and rear characters. Let's discuss certain ways in which this task can be performed. Method #1: Using
    4 min read
  • Count Number of Vowels using Sets in Given String - Python
    We are given a string and our task is to count the number of vowels present in it. Vowels in English include 'a', 'e', 'i', 'o', and 'u' (both uppercase and lowercase). Using sets, we can efficiently check for vowel presence due to their fast membership lookup. For example, if the input string is "B
    2 min read
  • Python Program to Count characters surrounding vowels
    Given a String, the task is to write a Python program to count those characters which have vowels as their neighbors. Examples: Input : test_str = 'geeksforgeeksforgeeks' Output : 10 Explanation : g, k, f, r, g, k, f, r, g, k have surrounding vowels. Input : test_str = 'geeks' Output : 2 Explanation
    3 min read
  • Python Program to Count the Number of Vowels in a String
    In this article, we will be focusing on how to print each word of a sentence along with the number of vowels in each word using Python. Vowels in the English language are: 'a', 'e', 'i', 'o', 'u'. So our task is to calculate how many vowels are present in each word of a sentence. So let us first des
    10 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences