Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Examples
  3. Count Vowels

Strings · Python example

Count Vowels

Count the vowels in a sentence with a loop and a membership test.

Open the compilerAll examples
beginnerstringsloopscount

A membership test with in checks whether a character belongs to a group. Lowercasing the text makes the count case-insensitive.

example.py
text = "The quick brown fox jumps over the lazy dog"
vowels = "aeiou"
count = 0

for char in text.lower():
    if char in vowels:
        count += 1

print(f"Vowels: {count}")

How the code works

  • The loop visits every character, including spaces.
  • Only vowel characters increase the counter.
  • The sentence contains 11 vowels, so the output is 11.

Key ideas

  • text.lower() produces a lowercase copy.
  • char in vowels is True for a, e, i, o, and u.
  • The accumulator count increases once per vowel.

Try changing it

  • Count consonants as well.
  • Ignore the letter y.
  • Use sum(1 for char in text.lower() if char in vowels).

Related examples

beginner

Capitalize Each Word

Transform a sentence with the title() string method.

intermediate

Word Frequency Counter

Count repeated words with a dictionary and print the results in alphabetical order.

Previous examplePalindrome CheckerNext exampleCapitalize Each Word

Expected output

Vowels: 11

Output is compared after trimming trailing whitespace on each line. Run the code to see it produced by CPython in your browser.

Practice next

Apply the same idea with automatic tests or browse a guided project.

ExercisesProjects