108

I'm a beginner with both Python and RegEx, and I would like to know how to make a string that takes symbols and replaces them with spaces. Any help is great.

For example:

how much for the maple syrup? $20.99? That's ricidulous!!!

into:

how much for the maple syrup 20 99 That s ridiculous
aaront
  • 1,215
  • 2
  • 10
  • 10
  • 15
    Strange this is marked as a duplicate of a question asked over a year later. – monkut Jan 30 '14 at 00:52
  • My advice is to read the documentation for the [re](http://docs.python.org/library/re.html) library. It includes some pretty good examples. – Jason Baker May 18 '09 at 02:00

3 Answers3

203

One way, using regular expressions:

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
  • \w will match alphanumeric characters and underscores

  • [^\w] will match anything that's not alphanumeric or underscore

Blue Ice
  • 7,888
  • 6
  • 32
  • 52
dF.
  • 74,139
  • 30
  • 130
  • 136
36

Sometimes it takes longer to figure out the regex than to just write it out in python:

import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')

If you need other characters you can change it to use a white-list or extend your black-list.

Sample white-list:

whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '

Sample white-list using a generator-expression:

whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
monkut
  • 42,176
  • 24
  • 124
  • 155
12

I often just open the console and look for the solution in the objects methods. Quite often it's already there:

>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'

Short answer: Use string.replace().

Nathan
  • 4,009
  • 2
  • 20
  • 21
buster
  • 1,071
  • 1
  • 10
  • 15