-1

the goal is to write a code that removes all of the vowels in a user inputted string; my code wont print a final new_inp string with no vowels. It seems that the line new_inp=inp.replace(ch,"") does not do anything. Anyone? Thank you.

inp="je m'en fou!"
vocals=['a','e','i','o','u']
for ch in inp.lower():
    if ch in vocals:
        new_inp=inp.replace(ch,"")
        print (new_inp)

5 Answers5

2

Here you go:

inp="je m'en fou!"
vocals=['a','e','i','o','u']
for ch in inp.lower():
    if ch in vocals:
        inp=inp.replace(ch,"")
        print (inp)

You were overwriting the string each iteration of the loop with the original string instead of accumulating changes.

robl
  • 74
  • 7
1

You need to accumulate all the updates in one string. Your current approach only replaces from the initial string and discards the previous updates in next iterations:

new_inp = inp
for ch in inp.lower():
    if ch in vocals:
        new_inp = new_inp.replace(ch,"")
        print(new_inp)

You can achieve the same using the following generator expression that filters out non vowel items:

new_inp = ''.join(x for x in inp if x not in vocals)
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
1

You could use the re module.

import re
inp="je m'en fou!"
re.sub('[aeiou]', '', inp)

Output:

"j m'n f!"
Billy
  • 5,179
  • 2
  • 27
  • 53
0

this is a way to do just that:

inp="je m'en fou!"
vocals='aeiou'

new_inp = ''.join(letter for letter in inp if letter not in vocals)
print(new_inp)  # j m'n f!

the problem with your approach is that you start with the original string in every iteration of the loop.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
0

Overwrite the inp variable.

inp="je m'en fou!"
vocals=['a','e','i','o','u']
for ch in inp.lower():
    if ch in vocals:
        inp=inp.replace(ch,'')
        print (inp)
Octane
  • 1,200
  • 11
  • 11