One feature that Python’s built-in string replacement facilities does not provide is case-insensitive string replacement. This is a reasonably useful construct (that I use fairly frequently in other languages, such as PHP), which I couldn’t find code for after Googling — so here’s some code that does it (licenced under the WTFPL, of course)

import re def ireplace(self,old,new,count=0): ''' Behaves like string.replace(), but does so in a case-insensitive fashion. ''' pattern = re.compile(re.escape(old),re.I) return re.sub(pattern,new,self,count) 

You can also subclass str in order to use it as a bound method:

import re class str_cir(str): ''' A string with a built-in case-insensitive replacement method ''' def ireplace(self,old,new,count=0): ''' Behaves like S.replace(), but does so in a case-insensitive fashion. ''' pattern = re.compile(re.escape(old),re.I) return re.sub(pattern,new,self,count)