1

I discovered today that the function string.replace(str1, str2) in Python 3 as well as Python 2 does not behave in the way I instinctively thought it would:

$ python3
Python 3.4.2 (default, Oct  8 2014, 10:45:20) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> str = ' not not not not Cannot not not not '.replace(' not ', ' NOT ')
>>> str
' NOT not NOT not Cannot NOT not NOT '

I understand why this happens: apparantly the replace function, once it finds a match, goes on on the first character after the previous found match which happens to be n in my case. Hence the second (and fourth...) not is never recognized, as the leading space is missing.

What is the standard way to replace strings to avoid the counter-intuitive behaviour above (so that all ␣not␣s are capitalized)?

I understand that I can split my string into takens, change the nots to NOTs and recombine it, but that is not what I am looking for. I suspect there is a proper replacement way available in Python.

6
  • 2
    Why you are not using str = ' not not not not Cannot not not not '.replace('not', 'NOT')? Commented Nov 3, 2015 at 17:45
  • 3
    Can't you just do ` str = ' not not not not Cannot not not not '.replace(' not', ' NOT')`? Replacing <space>NOT<space> with <space>NOT Commented Nov 3, 2015 at 17:45
  • 3
    Yes, str.replace is non-overlapping. Why not use a regex with word boundaries, and re.sub? Commented Nov 3, 2015 at 17:46
  • 1
    @Ahsanul Haque: I cannot do that cause then Cannot changes to CanNOT Commented Nov 3, 2015 at 17:50
  • 2
    @roymustang86: This would work in this example, but fail for example for not nothing as nothing would be changed as well Commented Nov 3, 2015 at 17:51

1 Answer 1

10
import re

s = re.sub(r"\bnot\b", "NOT", s)

Use a regular expression to match word boundaries rather than trying to match the spaces in between words.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.