Remove Punctuation With spaCy

Learn how to remove punctuation from text with the spaCy tokenizer, filter tokens in Python, and check the cleaned output for common NLP tasks.

Before you start

Removing punctuation with spaCy is a local text-processing task in a Python script or notebook. It does not require an internet service or an API key. You need Python with spaCy installed, plus a downloaded language model that supports tokenization. The examples on this page assume spaCy is version 3 or later and that the smaller English model is available.

Punctuation removal is reversible in the sense that you should keep the original text unchanged while building a separate cleaned token list. Do not overwrite your source text until you have verified the filtered output.

The main job is to tokenize text and filter out tokens whose type is punctuation. Non-punctuation characters such as emoji, symbols, digits, and whitespace may remain unless you add more rules.

Step-by-step instructions

Use these steps to remove punctuation with the spaCy tokenizer and keep the result as a list of tokens.

  • Load a spaCy model with nlp = spacy.load("en_core_web_sm"). If that model is not installed, download it first with python -m spacy download en_core_web_sm. Run the process through nlp.
  • Create a doc object by processing your text, for example doc = nlp("Hello, world! How are you?").
  • Build a filtered list with [token.text for token in doc if not token.is_punct]. The token.is_punct check removes tokens that spaCy recognizes as punctuation only.
  • Convert the list to a clean string with " ".join(clean_tokens) when you need sentence-like output. Keep the original text variable unchanged until you have checked the result.

For a pandas column, apply the same filter inside a function and assign the result to a new column. Keep punctuation removal as a filter after tokenization rather than changing the tokenizer behavior itself.

Check the output

After filtering, inspect the cleaned token list for characters that should be removed and for words that split in ways you did not expect. The tokenizer works on whitespace and language rules, so contractions and hyphenated words may be separated depending on the model.

A practical quality check is to print the first few tokens and compare them with the source text. If punctuation remains, verify that it is being treated as punctuation by spaCy and not as a symbol or another token type in your language model.

There is no universal punctuation pattern that fits every text. Currency symbols, quotation marks, dashes, and some technical characters may not be classified the same way in different models.

Common problems and next steps

If the punctuation removal seems incomplete, check token.is_punct first. A common correction is to use token.is_space and token.like_num as additional filters when you also want to remove whitespace or numbers.

If your text contains punctuation that spaCy does not label as punctuation, a custom matcher or a simple character filter may be a better next step. Keep the spaCy tokenizer as the text splitter, then remove unwanted token texts using a set of characters that you maintain yourself.

For reversibility, store the cleaned output separately from the original. If the cleaned string looks wrong, return to the unfiltered token list and adjust only the filter condition.