Instagram
youtube
Facebook
Twitter

Replace First Occurrence of Vowel with '-' in a String

A Python program to replace the first vowel in a string with a hyphen (-) while keeping the rest of the string unchanged.

Explanation in Points:

  1. Define Function: replace_first_vowel(s)
     
  2. Store Vowels: "AEIOUaeiou"
     
  3. Loop Through String: Check each character.
     
  4. Replace First Vowel: s[:i] + '-' + s[i+1:]
     
  5. Return Modified String: Print the result.

 

Program:

def replace_first_vowel(s):

    vowels = "AEIOUaeiou"

    for i in range(len(s)):

        if s[i] in vowels:

            return s[:i] + '-' + s[i+1:]

    return s  

s = input("Enter a string: ")

print("Modified string:", replace_first_vowel(s))