6.1 Know About the string Module

Sometimes, using the re module at all is a mistake. If you're matching a fixed string, or a single character class, and you're not using any re features such as the IGNORECASE flag, then the full power of regular expressions may not be required. The string module has several functions for performing operations with fixed strings and it's usually much faster, because the implementation is a single small C loop that's been optimized for the purpose, instead of the large, more generalized regular expression engine.

One example might be replacing a single fixed string with another one; for example, you might replace "word"with "deed". re.sub() seems like the function to use for this, but consider string.replace(). Note that string.replace() will also replace "word" inside words, turning "swordfish" into "sdeedfish", but the naïve RE word would have done that, too. (To avoid performing the substitution on parts of words, the pattern would have to be \bword\b, in order to require that "word" have a word boundary on either side. This takes the job beyond the string module's abilities.)

Another common task is deleting every occurrence of a single character from a string, or replacing it with another single character. You might do this with something like re.sub('\n', ' ', S), but string.translate() is capable of doing both these tasks, and will be much faster that any regular expression operation can ever be.

In short, before turning to the re module, consider whether your problem can be solved with the faster and simpler string module.