2

Is it possible to replace a single character inside a string that occurs many times?

Input:

Sentence=("This is an Example. Thxs code is not what I'm having problems with.") #Example input
                                 ^
Sentence=("This is an Example. This code is not what I'm having problems with.") #Desired output

Replace the 'x' in "Thxs" with an i, without replacing the x in "Example".

Ooker
  • 1,969
  • 4
  • 28
  • 58
Zephr Black
  • 21
  • 1
  • 2

3 Answers3

4

You can do it by including some context:

s = s.replace("Thxs", "This")

Alternatively you can keep a list of words that you don't wish to replace:

whitelist = ['example', 'explanation']

def replace_except_whitelist(m):
    s = m.group()
    if s in whitelist: return s
    else: return s.replace('x', 'i')

s = 'Thxs example'
result = re.sub("\w+", replace_except_whitelist, s)
print(result)

Output:

This example
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
0

Sure, but you essentially have to build up a new string out of the parts you want:

>>> s = "This is an Example. Thxs code is not what I'm having problems with."
>>> s[22]
'x'
>>> s[:22] + "i" + s[23:]
"This is an Example. This code is not what I'm having problems with."

For information about the notation used here, see good primer for python slice notation.

Community
  • 1
  • 1
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Perhaps, except With the particular code I'm dealing with, 22 and 23 vary, since my goal is to constantly change particular characters. for example, |_|_|_|X|_|_|_| is what I start with, Then I want to change it to |_|X|_|_|_|_|_| then afterward change again it to something like |_|_|_|_|_|_|X| so on forth, depending on how long the program is being run for. – Zephr Black Mar 23 '11 at 22:28
  • 2
    @Zephr Black Give a developed exemple, please. Your tiny explanation in a tiny comment is incomprehensible. Nobody whish to search for a code without being roughly sure that he understands well the wording – eyquem Mar 23 '11 at 23:51
0

If you know whether you want to replace the first occurrence of x, or the second, or the third, or the last, you can combine str.find (or str.rfind if you wish to start from the end of the string) with slicing and str.replace, feeding the character you wish to replace to the first method, as many times as it is needed to get a position just before the character you want to replace (for the specific sentence you suggest, just one), then slice the string in two and replace only one occurrence in the second slice.

An example is worth a thousands words, or so they say. In the following, I assume you want to substitute the (n+1)th occurrence of the character.

>>> s = "This is an Example. Thxs code is not what I'm having problems with."
>>> n = 1
>>> pos = 0
>>> for i in range(n):
>>>     pos = s.find('x', pos) + 1
...
>>> s[:pos] + s[pos:].replace('x', 'i', 1)
"This is an Example. This code is not what I'm having problems with."

Note that you need to add an offset to pos, otherwise you will replace the occurrence of x you have just found.

Giulio Piancastelli
  • 15,368
  • 5
  • 42
  • 62