Python Program to Replace First Vowel With ‘-‘ in String

In this tutorial, we will learn to write a Python program to replace the first occurrence of a vowel with a hyphen (‘-‘) in a given string. Suppose if some given input is “ram” then the output will look like “r-m”.

For Example:

Input: "Hello World"
Output: "H-ello World"

Input: "Python Programming"
Output: "Pyth-on Programming"

Program to Replace first occurrence of Vowel with ‘-‘ in String

In the below program, we are following the simple logic to replace the first occurrence of a vowel with ‘-‘ symbol in a string. We will iterate through the string character by character and check if it is a vowel. If we find Vowel the first time, we replace it with a hyphen and exit the loop.

def replace_vowel(string):
    vowels = "AEIOUaeiou"
    for i in range(len(string)):
        if string[i] in vowels:
            string = string[:i] + '-' + string[i+1:]
    return string

string = input("Please enter a string: ")
print("Original String:", string)
print("Modified String:", replace_vowel(string)) 


Please enter a string: quescol
Original String: quescol
Modified String: q-escol