Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace set or group of characters with string in Python

I am trying to make a simple script replacing all occurrences of certain group or set of characters (or set of strings) in text.

In this case I will try to replace all letters "a,e,i,o,u" with certain string.

My script:

def replace_all(text, repl):
    text1 = text.replace("a", repl)
    text2 = text1.replace("e", repl)
    text3 = text2.replace("i", repl)
    text4 = text3.replace("o", repl)
    text5 = text4.replace("u", repl)
    return text5

Is there any simpler way of doing it? What if I need to replace bigger group of chars or strings? Chaining it like this does not seem to be really effective then.

This is maybe a primitive question. However, I am still in learning phase so maybe I get it in later lessons. Thank you in advance for any advice.

like image 204
Blacho Avatar asked Jan 28 '23 20:01

Blacho


1 Answers

My knowledge tells me there are 3 different ways of doing this, all of which are shorter than your method:

  • Using a for-loop
  • Using a generator-comprehension
  • Using regular expressions

First, using a for-loop. This is probably the most straight-forward improvement to your code and essentially just reduces the 5 lines with .replace on down to 2:

def replace_all(text, repl):
    for c in "aeiou":
        text = text.replace(c, repl)
    return text

You could also do it in one-line using a generator-comprehension, combined with the str.join method. This would be faster (if that is of importance) as it is of complexity O(n) since we will go through each character and evaluate it once (the first method is complexity O(n^5) as Python will loop through text five times for the different replaces).

So, this method is simply:

def replace_all(text, repl):
    return ''.join(repl if c in 'aeiou' else c for c in text)

Finally, we can use re.sub to substitute all of the characters in the set: [aeiou] with the text repl. This is the shortest of the solutions and probably what I would recommend:

import re
def replace_all(text, repl):
    return re.sub('[aeiou]', repl, text)

As I said at the start, all these methods complete the task so there is no point me providing individual test cases but they do work as seen in this test:

>>> replace_all('hello world', 'x')
'hxllx wxrld'

Update

A new method has been brought to my attention: str.translate.

>>> {c:'x' for c in 'aeiou'}
{'a': 'x', 'e': 'x', 'i': 'x', 'o': 'x', 'u': 'x'}
>>> 'hello world'.translate({ord(c):'x' for c in 'aeiou'})
'hxllx wxrld'

This method is also O(n), so just as efficient as the previous two.

like image 163
Joe Iddon Avatar answered Mar 08 '23 17:03

Joe Iddon