Replace punctuation symbols in Python

An example of how to remove or replace punctuation symbols of a Python string

23/10/2012 Available in es
Python
Tips and Tricks

Following I will explain how to replace punctuation symbols by whitespace.

import re, string

def remove_punctuation ( text ):
  return re.sub('[%s]' % re.escape(string.punctuation), ' ', text)
Calling to previous function
>>> remove_punctuation ("El perro, de San Roque, no tiene rabo; ni nunca lo ha tenido.")
We will get this output
'El perro  de San Roque  no tiene rabo  ni nunca lo ha tenido '

We could make the function a little bit more generic to replace punctuation symbols by any other string.

import re, string

def replace_punctuation(text, replace):
  return re.sub('[%s]' % re.escape(string.punctuation), replace, text)
Calling the function to replace punctuation symbols by "[stop]"
>>> replace_punctuation(
  "El perro, de San Roque, no tiene rabo; ni nunca lo ha tenido.",
  '[stop]')

# output
'El perro[stop] de San Roque[stop] no tiene rabo[stop] ni nunca lo ha tenido[stop]'