-4

How to replace a string in python For example If the text is 'new words are newest n ew' and i want to replace 'new' word with 'y' If I use string.replace command The above text will change to 'y words are yest n ew' I don't want this to happen Ideally it should be 'y words are newest y' Could some body throw some light on how to achieve this using python.

Serenity
  • 35,289
  • 20
  • 120
  • 115
  • 1
    Have you tried anything yet? The [manual](https://docs.python.org/2/library/string.html) lists string functions. – scrappedcola Nov 13 '14 at 04:37
  • I dont see other option for string replacement in the [manual](https://docs.python.org/2/library/string.html). – Amar Reddy Nov 13 '14 at 04:39
  • 2
    search for `replace`, hint it's at the bottom of the page – scrappedcola Nov 13 '14 at 04:39
  • The other option i can go for is to use a dictionary. And make a exhaustive list of all the cases. But that doesnt seem to be an effective way. – Amar Reddy Nov 13 '14 at 04:40
  • So there is a special rule for "n ew". Are there any other special rules? You need to specify _exactly_ what is needed – John La Rooy Nov 13 '14 at 04:40
  • `string.replace(s, old, new[, maxreplace])` Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced. – scrappedcola Nov 13 '14 at 04:41
  • The text i mentioned is just an example. The replacement should happen only for the word that i needed. Here the word is 'new' – Amar Reddy Nov 13 '14 at 04:43
  • True!You can use dictionary! check this thread but you need to distinguish between different cases http://stackoverflow.com/questions/6116978/python-replace-multiple-strings – user3378649 Nov 13 '14 at 04:44
  • 'n ew' is also equivalent to 'new', ofcourse it has space between words. But i dont want 'newest' to be changed because it is a different word from 'new' – Amar Reddy Nov 13 '14 at 04:44
  • hi [scrappedcola](http://stackoverflow.com/users/462006/scrappedcola). I have already used the replace command and posted the result also in the question – Amar Reddy Nov 13 '14 at 04:46

2 Answers2

0
>>> s = 'new words are newest n ew'
>>> import re
>>> re.sub(r'\bn\s*e\s*w\b', 'y', s)
'y words are newest y'

'\bn\s*e\s*w\b' is a regular expression

\b means a word break
n, e, w mean the letter "n", "e", "w" respectively
\s* means zero or more whitespace characters

The regex given here may not be exactly what you need. You need to describe the rules exactly if you need a more (or less) restrictive regex.

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0

If you don't want to mess with regular expressions (although i suggest you do because they are so powerful) because they're hard to grasp, you could try something like the following:

Create a list with every word of your string and then iterate through the list and replace every word if it matches the string 'new'. Then print the list as a string (or you may assing it to a new/old variable the same way)

wordy = 'new words are newest n ew'
wordy_list = wordy.split()
for i, x in enumerate(wordy_list):
    if x == 'new':
        wordy_list[i] = 'y'
print(' '.join(wordy_list))

You could also write a one line lambda function which does the same thing like the following:

wordy = 'new words are newest n ew'
new_replace = lambda x: x if x != 'new' else 'y'
print(' '.join([new_replace(x) for x in wordy.split()]))