Whitespace Characters: List, Escape Codes and Removal
Whitespace characters are the characters that separate words and lines without drawing a symbol. The ordinary space, the tab and the line break are the familiar ones, but Unicode defines many more, and different programs disagree about which count. This is a quick reference for writers and programmers. It lists the whitespace characters with their codes and escape sequences, shows what common tools treat as white space, and explains how to remove each kind.
Whitespace characters at a glance
The characters below are visible placeholders: they take up room, and you can see the gap they leave.
| Character | Code | Escape | Use |
|---|---|---|---|
| Space | U+0020 | " " | The everyday word separator |
| Horizontal tab | U+0009 | \t | Indentation and columns |
| Line feed | U+000A | \n | Line break on Mac, Linux and the web |
| Carriage return | U+000D | \r | Windows uses \r\n; see carriage return |
| Vertical tab | U+000B | \v | Rare |
| Form feed | U+000C | \f | Page break in old text files |
| No-break space | U+00A0 | \u00a0 | A space that will not wrap |
| En space and em space | U+2002, U+2003 | \u2002, \u2003 | Typesetting widths |
| Thin space | U+2009 | \u2009 | Between a number and a unit, in some styles |
| Figure space | U+2007 | \u2007 | As wide as a digit, to line up numbers |
| Narrow no-break space | U+202F | \u202f | Used in French and other typography |
| Ideographic space | U+3000 | \u3000 | Full-width space in Chinese, Japanese and Korean text |
The full set of spaces between U+2000 and U+200A is in our guide to Unicode space characters.
Whitespace that moves the text to a new line
| Character | Code | Notes |
|---|---|---|
| Line feed | U+000A | The standard line ending on Unix, Mac and the web |
| Carriage return | U+000D | Old Mac line ending; half of the Windows ending |
| Next line | U+0085 | A control code used as a line break in some old systems |
| Line separator | U+2028 | A Unicode line break inside a paragraph |
| Paragraph separator | U+2029 | A Unicode paragraph break |
The difference between the Windows and Unix endings is covered in CRLF vs LF.
Invisible characters that are not whitespace
These look empty, but no whitespace check recognizes them.
| Character | Code | Why it is not whitespace |
|---|---|---|
| Zero-width space | U+200B | A format character with no width |
| Word joiner | U+2060 | A format character |
| Hangul filler | U+3164 | A letter |
| Braille pattern blank | U+2800 | A symbol |
See the guide to the blank character for how these behave.
What common tools treat as whitespace
Each language has its own definition. These results come from running the characters through the tools.
| Character | JavaScript \s and trim() | Python str.isspace() and split() |
|---|---|---|
| Space, tab, line feed, carriage return, form feed, vertical tab | Yes | Yes |
No-break space U+00A0 | Yes | Yes |
U+2000 to U+200A, U+202F, U+205F, U+3000 | Yes | Yes |
Line and paragraph separators U+2028, U+2029 | Yes | Yes |
Next line U+0085 | No | Yes |
Byte order mark U+FEFF | Yes | No |
U+001C to U+001F | No | Yes |
| Zero-width space, Hangul filler, braille blank | No | No |
Two practical lessons follow. First, never assume that a trim works the same way in two languages. Second, a trim in any of them leaves the zero-width space and the blank letters in place. Other tools are narrower still: Excel's TRIM function only deals with the ordinary space, character 32, and does not remove a no-break space. To fix that cell, replace character 160 first: =TRIM(SUBSTITUTE(A1, CHAR(160), " ")).
Escape sequences
Most programming languages write the common whitespace characters as backslash escapes inside strings.
\tis a tab,\na line feed and\ra carriage return in C, JavaScript, Python and most other languages.\v(vertical tab) and\f(form feed) work in C, JavaScript and Python.- JSON allows
\t,\n,\r,\fand\b, but not\v. Other characters need the form\u00a0. - For characters beyond U+FFFF, JavaScript uses
\u{...}and Python uses\U00012345. All whitespace characters in this guide are below that limit.
How to remove whitespace
Collapse repeated whitespace
# Python: handles tabs, newlines and special spaces
text = " ".join(text.split())
// JavaScript
text = text.replace(/\s+/g, " ").trim();
Both flatten line breaks into spaces. If you want to keep paragraphs, work line by line.
Remove all whitespace
# Python
text = "".join(text.split())
// JavaScript
text = text.replace(/\s+/g, "");
With the AI Text Cleaner
The cleaner does not act as a remove-all-whitespace tool. It does these things instead:
- Turns the no-break space, the spaces from
U+2000toU+200A,U+1680,U+202F,U+205FandU+3000into ordinary spaces. - Collapses runs of spaces and tabs inside a line to a single space, and keeps indentation at the start of a line.
- Trims spaces and tabs from line ends and trims the start and end of the text.
- Reduces three or more line breaks to one blank line.
- Turns Windows and old Mac line endings into line feeds.
Two behaviors are worth knowing. It deletes the vertical tab, the form feed and the next-line character as control codes instead of converting them to line breaks. And it leaves the line and paragraph separators U+2028 and U+2029 unchanged. See also how to remove extra spaces.
Whitespace in documents and on web pages
In a word processor, whitespace is shown on request. Press the ΒΆ button on the Home tab in Word, or Ctrl + Shift + 8 on Windows and Cmd + 8 on a Mac, and spaces, tabs and paragraph ends become visible marks. Our guide to showing hidden characters lists the symbols.
On a web page, the browser collapses runs of spaces, tabs and line breaks in the HTML source into a single space when it draws the text. That is why extra spaces typed into HTML do not show. A non-breaking space is the usual way to keep a gap, and the CSS rule white-space: pre keeps all whitespace as written. The entities for these are collected in our guide to the HTML space.
Common problems
- Mixed tabs and spaces. Code that looks aligned in one editor is crooked in another. Pick one and convert the other.
- Trailing whitespace. Spaces at the end of lines hide in diffs and in CSV fields. Trim every line.
- Special spaces that look normal. A no-break space cannot be told from a plain one by sight, and it defeats a search for a normal space.
- A trim that does nothing. The leftover is probably a zero-width character or a blank letter, which no white-space function covers.
Quick answers
How many whitespace characters are there? Unicode defines about two dozen with the White_Space property, including the ordinary space, tab, line breaks and the special spaces. Which ones a program treats as whitespace depends on the program.
Is a newline whitespace? Yes. A line feed and a carriage return both count.
Is a non-breaking space whitespace? Yes, in most languages. It is a space that does not allow a line break.