Python Practice › String Programs › Program 14

Repeat vowels 3 times and consonants 2 times

MediumString Programs · 14 of 106

Write a Python program that repeats each vowel 3 times and each consonant 2 times, leaving spaces alone.

Advertisement
Input
"Sqa Tools Learning"
Output
SSqqaaa TToooooollss LLeeeaaarrnniiinngg

Approach

  1. Choose the repeat count for each character and join the pieces.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs