← Back

Home › Python programs (Hindi)

String में Vowels Count करना Python में — code और output

Read this in English

Problem statement

एक sentence में कितने vowels (a, e, i, o, u) हैं गिनना — बड़े और छोटे अक्षरों को एक ही मानकर।

Approach 1: simple loop

Walk the string character by character and raise a counter whenever the character is one of the five vowels. The in operator checks membership in the vowel string directly.

text = "python programming"
vowels = "aeiou"
count = 0

for ch in text:
    if ch in vowels:
        count = count + 1

print(count)
# Output: 4

Every character gets exactly one question: "are you one of a, e, i, o, u?" The counter remembers how many times the answer was yes.

Approach 2: reusable function

Make it a function and normalise the case first — .lower() turns A into a so the uppercase vowels are counted without listing them twice.

def count_vowels(text):
    count = 0
    for ch in text.lower():
        if ch in "aeiou":
            count += 1
    return count

print(count_vowels("PYTHON"))              # 1
print(count_vowels("Education"))           # 5
print(count_vowels("xyz"))                 # 0

The function returns the number instead of printing it, so it can be used inside bigger programs — reports, filters, scoring.

Approach 3: Pythonic alternative

Python can count in one expression: sum() over a generator that yields 1 for each vowel. The condition is the same as before — only the bookkeeping is gone.

text = "python programming"

print(sum(1 for ch in text.lower() if ch in "aeiou"))   # 4
print(sum(text.lower().count(v) for v in "aeiou"))      # 4 — counts per vowel

The second form counts each vowel separately and adds the five results — handy when you want the breakdown per letter rather than the total.

Dry run

"hello" trace करें: h — vowel नहीं, counter 0। e — vowel, counter 1। l — नहीं। l — नहीं। o — vowel, counter 2। जवाब: 2। Function version में पहले .lower() होता है — इसलिए "PYTHON" पहले "python" बनता है और फिर वही हर-पर-एक-सवाल वाला trace चलता है। हर character के लिए एक row लिखो — dry run बस इतना ही है।

Common errors

Practice on PyDebug

Concept समझने के बाद इन free problems को browser में solve करें: