R337
R337

Reputation: 79

Python Beginners 3 - How do I create a loop that does this?

I want to create a loop that goes through a string, and if the letter 'r' follows a vowel, it would remove the vowel. For example:

Enter word: Cracker
New word: Crackr

or

Enter word: Flirk
New word: Flrk

I already have a way of doing this, but it is not very neat. I just want to know how I can improve myself. The way I do it is:

  if "ar" in s or "er" in s or "ir" in s or "or" in s or "ur" in s:
      s = s.replace("ar","r")
      s = s.replace("er","r")
      s = s.replace("ir","r")
      s = s.replace("or","r")
      s = s.replace("ur","r")

Thanks

Upvotes: 0

Views: 49

Answers (1)

jfs
jfs

Reputation: 414685

You could use regular expressions for this:

import re

s = re.sub("[aeiou]r", "r", s) # remove a vowel preceding `r`

Upvotes: 1

Related Questions