Is there any way to remove letters from a word starting with a Vowel? Python

47 views Asked by At

For every word in the sentence , If the word starts with a vowel, encode it as the first and last letter of the word. I don't know how to do it. Any help?

Example:- Iterator to iterate on each character of the input string Output should be : Ir to ie on eh character of the it string.

maybe use slicing?

2

There are 2 answers

0
Mureinik On

I'd split the sentence into words, and then for each word check what character it starts with. The, you can use a ternary expression to either return the first and last characters or the entire word:

original = 'Iterator to iterate on each character of the input string'

vowels = 'aeiouAEIOU'
result = ' '.join(w[0] + w[-1] if w[0] in vowels else w for w in orig.split(' '))
0
Shrei On
vowel="aiueo" #Making sure to list all of the vowels
sentence="Iterator to iterate on each" #Defining sentence
result=[i if i[0].lower() not in vowel else i[0]+i[-1] for i in sentence.split()] #checking for every words in the sentence and replacing each word with i[0] + i[1]if it contains vowel 
print(" ".join(result)) #Printing the result

Hope it could help :>