11

I want to replace all vowels in a string with a space:

string = str(input('Enter something to change'))
replacing_words = 'aeiou'

for i in replacing_words:
    s = string.replace('replacing_words', ' ')

print(s)

If this is a wrong code, could someone assist with right codes and explanation, why it didn't work?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
jeff
  • 910
  • 2
  • 6
  • 25

6 Answers6

15

You could define a translation table. Here's a Python2 code:

>>> import string
>>> vowels = 'aeiou'
>>> remove_vowels = string.maketrans(vowels, ' ' * len(vowels))
>>> 'test translation'.translate(remove_vowels)
't st tr nsl t  n'

It's fast, concise and doesn't need any loop.

For Python3, you'd write:

'test translation'.translate({ord(ch):' ' for ch in 'aeiou'}) # Thanks @JonClements.
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
6
  • You are using a literal 'replacing_words' instead of the variable i inside your for-loop.
  • You don't replace the original string to modify it again, instead you create a new string, resulting in only the last replacement to be shown

Here would be the correct code.

string = input('Enter something to change')
vowels = 'aeiouy'

for i in vowels:
    string = string.replace(i, ' ')

print(string)

Also, I think input returns a string 'type'. So calling str will have no effect. Not sure. Also #2: y is a vowel as well (so are åäö and other umlauts and weird characters if you want to be thorough).

Felix
  • 2,548
  • 19
  • 48
3

You are using the replace method incorrectly. Since you want to replace each of the characters separately, you should pass a single char every time.

Here is a one-liners that does the trick:

string = ''.join(' ' if ch in vowels else ch for ch in string)
Daniel Trugman
  • 8,186
  • 20
  • 41
  • If `str` isn't previously defined (and it'd be a bad choice of name as it's shadowing a builtin) then this'll error out on the `.replace` call. Also - that list-comp is just wrong... So this either won't run or won't work... – Jon Clements Oct 08 '17 at 09:57
  • The map/lambda is a bit overkill but it at least works now - have you considered just `''.join(' ' if ch in vowels else ch for ch in string)` ? – Jon Clements Oct 08 '17 at 10:28
1

Try this code:

string=raw_input("Enter your something to change")
replacing_words = 'aeiou'
for m in replacing_words:
    string=string.replace(m, ' ')
print string
0

In Python, strings are immutable.

# Python 3.6.1

""" Replace vowels in a string with a space """

txt = input('Enter something to change: ')
vowels = 'aeiou'
result = ''

for ch in txt:
    if ch.lower() in vowels:
        result += ' '
    else:
        result += ch

print(result)

Testing it:

Enter something to change: English language
 ngl sh l ng  g

In Python 3.x, you can also write (nothing to import):

vowels = 'aeiouAEIOU'
space_for_vowel = str.maketrans(vowels, ' ' * len(vowels))
print('hello wOrld'.lower().translate(space_for_vowel))

h ll  w rld
srikavineehari
  • 2,502
  • 1
  • 11
  • 21
0

You can first check in string if words are in vowel string then replace:

string = str(input('Enter something to change'))

replacing_words = 'aeiou'

for i in string:
    if i in replacing_words:
        string=string.replace(i," ")

print(string)

And if you want to keep original copy and also want to change string then:

string = str(input('Enter something to change'))
string1=string[:]
replacing_words = 'aeiou'

for i in string1:
    if i in replacing_words:
        string1=string1.replace(i," ")

print("{} is without vowels : {} ".format(string,string1))

output:

Enter something to change Batman
Batman is without vowels : B tm n 
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88