Python Practice › String Programs › Program 14
Repeat vowels 3 times and consonants 2 times
Write a Python program that repeats each vowel 3 times and each consonant 2 times, leaving spaces alone.
Input
"Sqa Tools Learning"
Output
SSqqaaa TToooooollss LLeeeaaarrnniiinngg
Approach
- Choose the repeat count for each character and join the pieces.
Solution
text = "Sqa Tools Learning"out = []for ch in text: if ch.lower() in "aeiou": out.append(ch * 3) elif ch.isalpha(): out.append(ch * 2) else: out.append(ch)print("".join(out))Output
SSqqaaa TToooooollss LLeeeaaarrnniiinngg
Line by line
Line 9
Building a list and joining once is faster than adding to a string in a loop.
Learn the concept
New to this topic? Read the lesson, then come back.