0

I have this line of code right now, it currently translates the normal word correctly. But I need to be able to remove a period from the end of the word, translate the word, then add the period to the end of the word.

def translateWordToPigLatin(word):    
    translatedWord = ""
     
    if len(word) > 0:
        # If word is a number...
        if word.isdigit():
            return word
        # end if
        # If first letter is a vowel...
        if word[0].lower() in vowels:
            # print("Word begins with a vowel.") # Do vowel stuff
            translatedWord = word + 'yay'
        else:   # If first letter is a consonant...
            # print("Word begins with a consonant.") # Do consonant stuff
            translatedWord = word[1:] + word[0] + "ay"
        # end if
    # end if
    return translatedWord.capitalize()
cavalcantelucas
  • 1,362
  • 3
  • 12
  • 34
  • Does this answer your question? [Best way to strip punctuation from a string](https://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string) – Chris Nov 03 '21 at 21:43
  • `re.sub` supports callables as a replacement – Marat Nov 03 '21 at 21:57

1 Answers1

0

Step 1) Split the parts appart

txt = "apple#banana#cherry#orange"
x = txt.split("#")
print(x)

>>> ['apple', 'banana', 'cherry', 'orange']

Step 2) Translate the word to pig Latin

Step 3) Put the parts together again

myTuple = ("John", "Peter", "Vicky")
x = "#".join(myTuple)
print(x)

>>> John#Peter#Vicky
cavalcantelucas
  • 1,362
  • 3
  • 12
  • 34