Where extra spaces come from
Extra spaces sneak in from almost every copy and paste. PDFs pad justified lines and columns with spaces. Web pages use non-breaking spaces that look normal but behave differently. Spreadsheets pad cells, old documents use two spaces after every period, and text lined up by hand with the space bar turns into a mess as soon as the font changes.
Cleaning does three things: runs of spaces become one space, tabs become spaces, and (if you choose) each line is trimmed at both ends and empty lines are dropped. Single spaces between words are left alone.
The whitespace characters you can't see
| Character | Looks like | Usually comes from | What cleaning does |
|---|---|---|---|
| Space (U+0020) | A normal gap | Typing | Runs of two or more become one |
| Non-breaking space (U+00A0) | Exactly the same gap | Web pages, word processors | Runs become one normal space |
| Tab (U+0009) | A wide gap | Spreadsheets, code | Becomes a space |
| Line break | A new line | The Enter key, PDFs | Kept; empty lines can be removed |
| Zero-width space (U+200B) | Nothing at all | Some web pages and apps | Not touched; see the tip below |
If two words still refuse to match after cleaning, a zero-width space may be hiding between letters. In Find and Replace, turn on Regular expression, search for \u200B and replace it with nothing.
Why stray spaces break spreadsheets and searches
To a computer, “Smith” and “Smith ” with a trailing space are different values. That one invisible character makes spreadsheet lookups return errors, stops duplicate checks from matching, and makes a pasted discount code or tracking number fail. Trimming each line fixes all of these at once.
Example: A line copied from an invoice PDF
- Original: two leading spaces, “Total:”, three spaces, “42 items”, two trailing spaces (21 characters)
- Cleaned: “Total: 42 items” (15 characters)
6 invisible characters removed, and the line now matches “Total: 42 items” anywhere else.
Codes are the worst case. A gift card number, booking reference or tracking number copied from an email often brings a trailing space along. Plenty of forms reject it with a vague “invalid code” message, and you can stare at it for minutes without seeing anything wrong. Paste it here, trim, and copy it back.
Cleaning text from PDFs, emails and spreadsheets
Each source leaves its own kind of mess, so it helps to know what you're dealing with before you paste.
| Source | Typical problem | What fixes it |
|---|---|---|
| Runs of spaces from justified text, plus a line break at the end of every line | Clean spaces here, then join lines | |
| Web page | Non-breaking spaces and blank lines between every paragraph | Clean spaces with empty lines removed |
| Reply-quoted email | Indented lines and double spaces after periods | Trim each line and collapse spaces |
| Spreadsheet column | Trailing spaces in cells, tabs between columns | Trim each line; keep a copy if you need the tabs |
Spaces before punctuation are a different problem: “Hello , world !” has single spaces, so collapsing runs won't touch them. In Find and Replace, turn on Regular expression, find \s+([,.;:!?]) and replace it with $1. The result is “Hello, world!”.
One space or two after a period
The two-space habit dates from typewriters, where every letter took the same width and an extra space made sentence breaks easier to see. Proportional fonts already space punctuation well, and most current style guides, including Chicago and APA (since its 7th edition), call for one space.
Because cleaning collapses every run of spaces, it converts a whole document from two spaces to one in a single pass. If a teacher or publisher really does want two, keep your original and don't clean it.
Double spacing also inflates character counts by one per sentence. A 300-sentence report typed that way carries about 300 extra characters, which matters when an abstract or application form has a character limit.
When not to remove spaces
Some text depends on its spacing. Cleaning it would quietly break the very thing you need, so keep an untouched copy of anything on this list.
- Python and YAML files, where indentation is part of the meaning. Trimming lines removes it.
- Tab-separated data copied from a spreadsheet. Tabs become spaces, so columns merge when you paste back.
- Fixed-width tables or text art lined up with spaces.
- Poetry or song sheets with deliberate indents.
For text from PDFs, cleaning spaces is usually half the job. Follow it with Remove Line Breaks to rejoin sentences split across lines, and Remove Duplicate Lines if you're cleaning a list.