I'm doing the CS50P course as my first course in python and in Problem set 2 there was a problem about removing vowels from a given text
I tried doing
Vowels = ["A", "a", "E", "e", "U", "u", "O", "o", "I", "i"]
inputs = input("input: ")
outputs = inputs
for x in Vowels:
outputs.replace(x, "")
print(f"output: {outputs}")
but it didn't work I've solved the problem in another way but I'm just curious why didn't my first attempt work
CodePudding user response:
The replace function does not actually replace it, it just returns a string with the inputs replaced. So instead, you could write:
Vowels = ["A", "a", "E", "e", "U", "u", "O", "o", "I", "i"]
inputs = input("input: ")
outputs = inputs
for x in Vowels:
outputs = outputs.replace(x, "")
print(f"output: {outputs}")
CodePudding user response:
You need to reassign the value, after replacing the vowel. So your code will be
outputs = outputs.replace(x, "")
The replace function doesn't modify the original string, instead it returns the copy of modifying string.