Replace multiple characters in a string at once
Question:
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?
Answers:
- 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).
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)
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
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.
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
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
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?
- 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).
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)
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
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.
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
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