I'm trying to create a function that accepts a string and replaces the vowels with other characters.
I've written some code, but when I run it in IDLE the output is 'None', but I'm expecting it to be '44 33 !! ooo000 ||||'
I have the below:
def vowel_swapper(string):
for char in string:
if char in 'aeiouAEIOU':
char.replace('a', '4').replace('A', '4').replace('e', '3').replace('E', '3')\
.replace('i', '!').replace('I', '!').replace('o', 'ooo').replace('O', '000').replace('u', '|_|').replace('U', '|_|')
print(vowel_swapper("aA eE iI oO uU"))
Where have I gone wrong here?
Edit: Thanks for all of the responses. I will also take on the advice about using a dictionary and look into it.
vowel_swapperfunction doesn't return anything, so you can expect your print statement to printNone(functions that don't explicitly return something always implicitly returnNone). You also haven't shown the entirety of thevowel_swapperfunction, so I'm assuming the first few lines of your code snippet make up the body of that function. Also, strings are immutable in python, andstr.replacedoesn't modify a string in-place. Additionally, there is no need to iterate over the characters in your string. Simply usereturn string.replace(...).replace(...)...str.replace()constructs in each call new string copy. this solution is enefficeient. if you dont have to use replace method then this we can solve it in list comrehension without the ovverhead of replace