coleslaw117
coleslaw117

Reputation: 19

A translator that replaces vowels with a string

For those that don't know, replacing vowels with 'ooba' has become a popular trend on https://reddit.com/r/prequelmemes . I would like to automate this process by making a program with python 2.7 that replaces vowels with 'ooba'. I have no idea where to get started

Upvotes: 1

Views: 65

Answers (3)

sawyermclane
sawyermclane

Reputation: 1066

For a quick and simple answer, you could feed string meme into here

for i, c in enumerate(meme):
  if c in ['a', 'e', 'i', 'o', 'u']:
    meme[:i] = meme[:i] + 'ooba' + meme[i+1:]

It goes over each character in the string, and checks if it is a vowel. If it is, it slices around the index and inserts 'ooba' where it used to be.

Upvotes: 0

Jithin Pavithran
Jithin Pavithran

Reputation: 1351

Following method is suggested if the line is short. I would prefer using regex otherwise. Following assumes that your text is s.

s = ''.join(['ooba' if i in ['a', 'e', 'i', 'o', 'u'] else i for i in s])

Regex approach:

import re
s = re.sub(r'a|e|i|o|u', "ooba", s)

Upvotes: 1

You could use a simple regular expression:

import re

my_string = 'Hello!'
my_other_string = re.sub(r'[aeiou]', 'ooba', my_string)

print(my_other_string)  # Hooballooba!

Upvotes: 1

Related Questions