+ 1
Python: Given a sentence, print the sentence with all vowels removed.
For example: x='the quick brown fox' ı want to print this: th qck brwn fx my code: x='the quick brown fox' y=x.split() list1=[a,e,ı,i,o,ö,u,ü] # letters in list are string for i in y: for k in i: if k in list1: u=i.index(k) l=i.replace(i[u],"") print(l) my codes result: th qick quck brwn fx when the word has more than one vowel its not giving me what ı want. How can ı fix this?
5 Réponses
+ 5
No need to break sentence into a list of words and then loop over char of each word in the list.
Simply loop over the string.
https://code.sololearn.com/cDGTYdD06X1r/?ref=app
+ 4
The reason that the following vowel is not removed is because when you use
i.replace(i[u], ")
The position of the letters shifted.
So after u, the next processed char jumps to c
Same for those not noticeable, for example
after e, it should check space, but space is skipped, and q is checked instead.
So, instead of modifying the original string in the for loop,
you should create a template empty string before for loop.
result = ""
Then, in for loop, when the char is not vowel, the char is pushed to string
result = result + i
After the for loop, prints the result string.
+ 4
Gordon afaik replace() method returns a copy of a string instead of doing changes to the original one.
So I think the real problem here was that, everytime the word was printing without the vovel but next time again complete word was checked for other vovel.
+ 3
I don't know how you even got your expected output as according the code you gave here, it should print the word "quick" twice by removing 2 different vovels each time.
here is the original one 👇
https://code.sololearn.com/csbj6SS3V9zz/?ref=app
and here is the fix👇
https://code.sololearn.com/cEgUb5yK37Di/?ref=app
+ 2
Arsenic thanks for pointing out my mistake. Yes Python replace() do not modify original string. I mixed up Python replace() and JavaScript replace() sorry.
https://code.sololearn.com/caJguIDPO054/?ref=app
As replace() is used to replace all occurrence, the code snippet can be as simple as
for vowel in "aeiou" :
x = x.replace(vowel, "")
print(x)
https://code.sololearn.com/culJmh556FRn/?ref=app