Reputation: 11
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?
Upvotes: -2
Views: 66
Reputation: 1
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 :>
Upvotes: 0
Reputation: 312136
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(' '))
Upvotes: 0