Remove Punctuation in Python Without Imports
Learn how to remove punctuation from a string in Python without importing modules. Step-by-step examples for cleaning text from audio transcripts, image OCR, and video captions.
Before you start
You can remove punctuation from a string in Python using only built-in string methods and a simple loop or generator expression. No third-party libraries or standard library imports are required, so this process runs entirely locally in your Python environment. You need a string that contains the text you want to clean, such as a line from an audio transcript, a caption generated from a video, or text extracted from an image with OCR.
The approach works the same in Python 3.6 and newer versions. It treats punctuation as any character listed in the string.punctuation constant, but since you are avoiding imports, you will define the punctuation characters manually or use the str.isalnum and str.isspace methods to identify characters to keep.
Step-by-step instructions
The simplest reversible approach is to build a new string that keeps only letters, numbers, and spaces. Start with your original string unchanged, then apply a loop or generator expression to filter out unwanted characters.
Use a loop to keep alphanumeric characters and spaces
Create an empty result string. Iterate over each character in the original string and check whether the character is alphanumeric or a space. If it is, append the character to the result; otherwise skip it. This preserves words and spacing while removing punctuation marks such as commas, periods, exclamation points, and question marks.
For example, for a string like "Hello, camera! How are you?", the loop keeps "Hello camera How are you". The comma, exclamation point, and question mark are dropped, and the spaces remain intact.
Use a generator expression for a shorter version
A more compact version uses the join method with a generator expression: ''.join(ch for ch in text if ch.isalnum() or ch.isspace()). This produces the same result as the explicit loop. It is useful when you want the cleaning logic to fit on one line and still avoid importing anything.
Preserve apostrophes or hyphens if needed
If your media text contains contractions such as "don't" or compound words such as "high-resolution", you may want to keep apostrophes or hyphens. In that case, add the specific characters to the allowed condition. For example, allow a character when ch.isalnum() or ch.isspace() or ch in "'-"". This gives you control over which punctuation marks remain.
Check the output
Verify that the result contains only the characters you intended to keep. Print the cleaned string and compare it with the original. Check word boundaries and spacing. If two punctuation marks surrounded a word, the replacement leaves the surrounding spaces as they were, which can occasionally create extra gaps. For example, removing the comma and period from "one, two." leaves "one two", which is usually fine, but you should inspect lines with dashes or repeated punctuation to confirm the spacing looks natural.
This method does not change the original string, so you can keep it for reference or process the same input again with different rules. The output is plain text without any special formatting or encoding changes.
Common problems and next steps
Punctuation remains in the output
If some punctuation still appears, the allowed condition may be too broad. The str.isalnum method returns True for letters and numbers, but in some cases Unicode marks may be classified differently depending on the Python version. Check the specific character by printing its Unicode code point or by testing it directly with the condition. Replace the generic isalnum check with an explicit allowed set if you need stricter control over non-English text from localized audio or image sources.
Spaces or line breaks disappear
If spaces are missing from the result, the condition is probably only checking for alphanumeric characters. Add ch.isspace() to the allowed condition. This keeps spaces, tabs, and newlines, which is important for preserving sentence boundaries in transcripts and captions.
Contractions or hyphenated terms are split
When apostrophes or hyphens are removed, words like "don't" become "dont" and "high-resolution" becomes "highresolution". To avoid this, include the specific characters you want to preserve in the condition. Keep this rule consistent across all text from the same transcription or OCR source.
You need to remove only selected punctuation
Instead of filtering by what to keep, define a string of punctuation marks to remove and replace each with an empty string. For a direct replace approach, you can loop over the punctuation characters and call text = text.replace(punct, ''). This is less efficient for long strings but gives precise control when you only want to delete a few marks.
Once the string is clean, you can pass it to other Python text processing steps, such as tokenization for subtitles, keyword extraction from video descriptions, or basic search matching in a media library index.