Reputation: 85
I am trying to remove the occurrences of the vowels in the string, except if they are the starting of a word. So for example an input like "The boy is about to win"
should ouput Th by is abt t wn
.Here is what I have so far. Any help would be appreciated!
def short(s):
vowels = ('a', 'e', 'i', 'o', 'u')
noVowel= s
toLower = s.lower()
for i in toLower.split():
if i[0] not in vowels:
noVowel = noVowel.replace(i, '')
return noVowel
Upvotes: 3
Views: 2005
Reputation: 42421
One approach is to use a regular expression that replaces vowels not preceded by a word boundary. Also, you might want to think about some more interesting test cases if your code is supposed to handle arbitrary text with various types of punctuation.
import re
s = "The boy is about to win (or draw). Give him a trophy to boost his self-esteem."
rgx = re.compile(r'\B[aeiou]', re.IGNORECASE)
print rgx.sub('', s) # Th by is abt t wn (or drw). Gv hm a trphy t bst hs slf-estm.
Upvotes: 6
Reputation: 113914
Try:
>>> s = "The boy is about to win"
>>> ''.join(c for i, c in enumerate(s) if not (c in 'aeiou' and i>1 and s[i-1].isalpha()))
'Th by is abt t wn'
The key part of the above if the generator:
c for i, c in enumerate(s) if not (c in 'aeiou' and i>1 and s[i-1].isalpha())
The key part of the generator is the condition:
if not (c in 'aeiou' and i>1 and s[i-1].isalpha())
This means that all letters in s
are included unless they are vowels that are not either (a) at the beginning of s
and hence at the beginning of a word, or (b) preceded by a non-letter which would also mean that they were at the beginning of a word.
for
loopdef short(s):
new = ''
prior = ''
for c in s:
if not (c in 'aeiou' and prior.isalpha()):
new += c
prior = c
return new
Upvotes: 1
Reputation: 174766
Through re.sub.
>>> import re
>>> s = "The boy is about to win"
>>> re.sub(r'(?i)(?<=\S)[aeiou]', r'', s)
'Th by is abt t wn'
\S
matches any non-space character.
Upvotes: 0
Reputation: 1456
>>> re.sub('(?<=\w)[aeiou]','',"The boy is about to win")
'Th by is abt t wn'
Upvotes: 0
Reputation: 54223
Use a regular expression:
import re
re.sub("(?<!\b)[aeiouAEIOU]", '', s)
Upvotes: 0
Reputation: 53535
You can use regex on the rest of the string (ignoring the first character):
import re
s = 'The boy is about to win'
s = s[0] + re.sub(r'[aeiou]', '', s[1:])
print s # Th by s bt t wn
Upvotes: 0