I'm new to python and I'm trying to create a code for a course where I have to replace the second occurrence of each letter with something else such as:
print(vowel_swapper("aAa eEe iIi oOo uUu")) Should print "a/\a e3e i!i o000o u/u" to the console
The code works for this string but when I do the second:
print(vowel_swapper("Hello World")) Should print "Hello Wooorld" to the console
it outputs "Hell000 W000rld". I'm sure that the way I'm doing it won't be very efficient, since I'm new to it I'm not expecting it to be efficient I'm just trying to learn how to make it do what I want it to do.
def vowel_swapper(string):
replace1 = ["a", "A"]
replace2 = ["e", "E"]
replace3 = ["i", "I"]
replace4 = ["o", "O"]
replace5 = ["u", "U"]
new_string = string
count = 0
for i in range(0, len(string)):
if (string[i] == replace1[0]) or (string[i] == replace1[1]):
count = count + 1
if count == 2:
new_string = new_string.replace(string[i], '/\\')
count = 0
for i in range(0, len(string)):
if (string[i] == replace2[0]) or (string[i] == replace2[1]):
count = count + 1
if count == 2:
new_string = new_string.replace(string[i], "3")
count = 0
for i in range(0, len(string)):
if (string[i] == replace3[0]) or (string[i] == replace3[1]):
count = count + 1
if count == 2:
new_string = new_string.replace(string[i], "!")
count = 0
for i in range(0, len(string)):
if (string[i] == replace4[0]) or (string[i] == replace4[1]):
count = count + 1
if count == 2:
new_string = new_string.replace(string[i], "000")
count = 0
for i in range(0, len(string)):
if (string[i] == replace5[0]) or (string[i] == replace5[1]):
count = count + 1
if count == 2:
new_string = new_string.replace(string[i], "\\/")
return new_string