0

This is my solution so far:

def chemificateWord(word):

vowels = ('a', 'e', 'i', 'o', 'u', 'y','A','E','I','O','U','Y')

word2 = []

for letter in word:

    if not word.endswith(vowels):
        word2 = word + 'ium'
    return word2 

''' So when I now print(chemificateWord(Californ))

The result I want is:

Californ**ium**

So I want to replace to last ending letters if they are vowels with 'ium', could anyone help? '''

Akshath Mahajan
  • 248
  • 2
  • 11
Robin Vdc
  • 11
  • 3
  • Here's a hint: https://stackoverflow.com/questions/914715/how-to-loop-through-all-but-the-last-item-of-a-list – sshashank124 Dec 28 '19 at 14:06
  • This kind of removal is exactly what `.rstrip` does. There are tons of people who accidentally use it wrongly and don't want it to work the way that it does; this is the rare exception that is the other way around. – Karl Knechtel Mar 29 '23 at 08:02

2 Answers2

3
def chemificateWord(word):    
    vowels = ('a', 'e', 'i', 'o', 'u', 'y','A','E','I','O','U','Y')

    word2 = word

    while word2.endswith(vowels):
        word2 = word2[:-1]
    word2 += "ium"
    return word2
Boendal
  • 2,496
  • 1
  • 23
  • 36
  • Thank you for the answer. Additional, if I would like to replace all ending vowels by 'ium'? So in this case this would be replacing 'ia' by 'ium'. (and even if there are more ending vowels than 2) – Robin Vdc Dec 28 '19 at 14:14
  • @RobinVdc see the new solution – Boendal Dec 28 '19 at 14:20
1

You might use re module for this task following way:

import re
def chemicate(word):
    return re.sub(r'[aeriouyAEIOUY]*$', 'ium', word)

Then:

print(chemicate('Californ'))  # Californium
print(chemicate('Baaa'))  # Bium

First argument of re.sub is so-called pattern, [] denotes any of characters inside (vowels in this case), * denotes 0 or more repeats, $ is zero-length assertion and denotes end of string.

Daweo
  • 31,313
  • 3
  • 12
  • 25