Skip to content
Programs · Program 8-2

Q.Program 8-2: Write a program with a user defined function with string as a parameter which replaces all vowels in the string with '*'.

Telangana TsbieTextbookSubjective· 3mImportance★★★★★est
23% · 3/13 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Build a new string character by character: if the character is a vowel (test with membership in "aeiouAEIOU"), append '*', otherwise append the character itself.

Idea. Strings are immutable, so the function constructs a new string. The elegant part is the vowel test: instead of ten comparisons chained with or, one membership expression — character in 'aeiouAEIOU' — checks both lowercase and uppercase vowels at once.

def replaceVowel(st):
    """Replace all vowels in the string with '*'."""
    newStr = ""
    for character in st:
        if character in 'aeiouAEIOU':   # is it a vowel (either case)?
            newStr += '*'
        else:
            newStr += character
    return newStr

# main program
st = input("Enter a string: ")
st1 = replaceVowel(st)
print("The original string is:", st)
print("The modified string is:", st1)

Trace for st = "Hello World":

charactervowel?newStr
HnoH
eyesH*
lnoH*l
lnoH*ll
oyesHll
(space)no"Hll "
WnoHll W
oyesHll W*
rnoHll W*r
lnoHll W*rl
dnoHll W*rld

Sample run

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.