TIMEX
TIMEX

Reputation: 271854

How do I replace all punctuation in my string with "" in Python?

If my string was:

Business -- way's

I'd like to turn this into:

Business  ways

ie. replace NON abc/123 into ""

Upvotes: 0

Views: 5636

Answers (3)

genio
genio

Reputation: 882

(regular expression) replace

[[:punct:]]

with '' (if Python supports that).

[] is a character class, [::] is posix class syntax. [:punct:] is punctuation, so the character class for all punctuation marks would be [[:punct:]]

An alternate way of the same thing is \p and friends: \p{IsPunct}

See just below "Character Classes and other Special Escapes" in http://perldoc.perl.org/perlre.html (yes, I know it's a Perl document, but this is more about regular expressions than Perl).

That being said, the first answer with [^\w\s] answers what you explained a little more explicitly. This was more just an explanation of how to do what your question asked.

Upvotes: -3

DisplacedAussie
DisplacedAussie

Reputation: 4694

Or, if you don't want to use a regular expression for some reason:

''.join([x for x in foo if x.isalpha() or x.isspace()])

Upvotes: 6

Kenan Banks
Kenan Banks

Reputation: 211990

Simple regular expression:

import re

>>> s = "Business  -- way's"
>>> s = re.sub(r'[^\w\s]', '', s)
>>> s
"Business  ways"

Upvotes: 16

Related Questions