In this post, you will learn a python program to replace all the vowels with a given character, special symbol (*), number, or space with a detailed explanation and example.
Question may be asked like:
Write a python program to replace all vowels of the String with a given character
But before we write a program few programming concepts you have to know:
Source Code:
my_string = input('ENter your String : ')
my_char = input('ENter your character : ')
vowels = ['a','e','i','o','u','A','E','I','O','U']
new_string = ''
for i in my_string:
if i in vowels:
new_string +=my_char
else:
new_string += i
print(new_string)
Output:
ENter your String : allinpython ENter your character : % %ll%npyth%n |
Let’s solve a few more problems related to this for practice.
Write a python program to replace all vowels with ‘*’
Before writing this program you should know about:
Source Code:
my_str = input('ENter your String : ')
my_char = '*'
vowels = ['a','e','i','o','u','A','E','I','O','U']
new_str = ''
for i in my_str:
if i in vowels:
new_str +=my_char
else:
new_str += i
print(new_str)
Output:
ENter your String : this is the demo th*s *s th* d*m* |
Write a python program to replace all vowels with a given Number Using a while loop
Before writing this program you should know about:
Source Code:
string = input('Enter your string : ')
num = int(input('Enter your number : '))
vowels = ['a','e','i','o','u','A','E','I','O','U']
result = ''
i = 0
while i < len(string):
if string[i] in vowels:
result += str(num)
else:
result += string[i]
i+=1
print(result)
Output:
Enter your string : Learn python programming Enter your number : 9 L99rn pyth9n pr9gr9mm9ng |
Write a python program to replace all vowels with a Space Using a while loop
Before writing this program you should know about:
Source Code:
input_string = input('Enter your string : ')
space = ' '
vowels = ['a','e','i','o','u','A','E','I','O','U']
result = ''
i = 0
while i < len(input_string):
if input_string[i] in vowels:
result += space
else:
result += input_string[i]
i+=1
print(result)
Output:
Enter your string : allinpython ll npyth n |
Python program to replace the first occurrence of a vowel with a given Character
Before writing this program you should know about:
Source Code:
input_string = input('Enter your string : ')
my_char = input('Enter your character: ')
vowels = ['a','e','i','o','u','A','E','I','O','U']
for i in vowels:
v_index = input_string.find(i) #find index of vowel (first occurrence)
if v_index >= 0:
break
input_string = list(input_string) # convert string into list
input_string[v_index] = my_char # replace vowel
new_str = ''.join(input_string) # cobvert list in string again
print(new_str) # result
Output:
Enter your string : raama Enter your character: & r&ama |