Python Loops Examples
Problem 1: Count the Number of Vowels in a String
Example 1
text = "Python Programming"
vowels = "aeiouAEIOU"
count = 0
for ch in text:
if ch in vowels:
count += 1
print("Vowel Count:", count) Problem 2: Remove All Vowels from a String
Example 2
text = "Beautiful"
vowels = "aeiouAEIOU"
result = ""
for ch in text:
if ch not in vowels:
result += ch
print("After Removing Vowels:", result) Problem 3: Print all odd numbers from 1 to 30
Example 3
for i in range(1, 30):
if i % 2 != 0:
print(i,end=" ") Problem 4: Count Occurrence of Each Character
Example 4
text = "banana"
for ch in set(text):
print(f"{ch} - {text.count(ch)}") Problem 5: Check if a number is prime
Example 5
num = int(input(“Enter Number: “))
is_prime = True
if num > 1:
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print("Prime")
else:
print("Not Prime")