Identify and distinguish Unicode whitespace characters

How to tell Unicode whitespace characters apart by code point, what normalizing or replacing them changes, and when a plain ASCII space is the safer choice.

Two pieces of text can look identical on screen and still fail to match. Very often the cause is the gap between words, which can be any of a couple of dozen Unicode whitespace characters, or an invisible character that is not whitespace at all. This review explains how to identify which character you have, what changes when you replace or normalize it, and when the ordinary space is the better choice.

Short answer: when to keep special spaces and when to replace them

Converting unusual whitespace to the ordinary space (U+0020) suits most practical work: search indexes, database keys, usernames, file names, code, spreadsheet data and anything that is compared or matched. The benefit is predictability, because identical-looking text becomes identical in storage. The cost is that you lose behavior that some spaces exist to provide. A no-break space keeps a number attached to its unit at a line end, and a narrow no-break space carries typographic conventions in some languages and locale formats. Keep those characters only in typeset prose or formatted output where line-breaking and spacing matter, and only if the destination handles them. In every other case, plain text with ordinary spaces is the safer default.

What counts as a Unicode whitespace character

"Whitespace" has no single meaning in practice. Unicode defines a White_Space character property, and it also has general categories: Zs (space separator), Zl (line separator) and Zp (paragraph separator). Programming languages, regular-expression engines and applications then apply their own definitions, which overlap with Unicode's but do not match it exactly. When you identify a character, record its code point and general category, not just whether some tool calls it a space.

The White_Space property covers the Zs characters, the two separators U+2028 and U+2029, and the control characters tab, line feed, vertical tab, form feed, carriage return and next line (U+0085). Several characters that people casually call spaces are outside that set, including the zero width space.

Code point | Name | Category | Unicode White_Space | Distinguishing behavior
U+0009 | CHARACTER TABULATION | Cc | Yes | Width depends on tab stops, not the font
U+000A | LINE FEED | Cc | Yes | Line break
U+000D | CARRIAGE RETURN | Cc | Yes | Line break, often paired with LF
U+0085 | NEXT LINE | Cc | Yes | Line break in some legacy encodings
U+0020 | SPACE | Zs | Yes | Ordinary space, allows a line break
U+00A0 | NO-BREAK SPACE | Zs | Yes | Prevents a line break; not collapsed by HTML
U+1680 | OGHAM SPACE MARK | Zs | Yes | May render as a visible stroke in Ogham fonts
U+2000 to U+200A | EN QUAD through HAIR SPACE | Zs | Yes | Fixed nominal widths such as en, em, thin and hair; actual width depends on the font
U+2007 | FIGURE SPACE | Zs | Yes | Width of a digit; does not allow a line break
U+202F | NARROW NO-BREAK SPACE | Zs | Yes | Narrow and non-breaking; used in some locale formats for numbers, dates and times
U+205F | MEDIUM MATHEMATICAL SPACE | Zs | Yes | Spacing in mathematical text
U+3000 | IDEOGRAPHIC SPACE | Zs | Yes | Width of a CJK ideograph
U+2028 | LINE SEPARATOR | Zl | Yes | Treated as a line terminator in JavaScript
U+2029 | PARAGRAPH SEPARATOR | Zp | Yes | Paragraph break
U+200B | ZERO WIDTH SPACE | Cf | No | No width; marks a possible line break
U+200C / U+200D | ZERO WIDTH NON-JOINER / JOINER | Cf | No | Control how neighboring characters join; required in some scripts and emoji sequences
U+2060 | WORD JOINER | Cf | No | No width; prevents a line break
U+FEFF | ZERO WIDTH NO-BREAK SPACE (byte order mark) | Cf | No | Often appears at the start of files; JavaScript still treats it as whitespace
U+3164 | HANGUL FILLER | Lo | No | Classified as a letter; often used by blank-text generators
U+2800 | BRAILLE PATTERN BLANK | So | No | A symbol that displays as empty; also used for blank text

The last rows clear up a common confusion. Tools that offer "invisible text", "blank characters" or an "empty font" usually insert characters like U+3164 or U+2800. These are not fonts, and Unicode does not classify them as whitespace. They are letters or symbols that happen to have no visible ink. Trimming functions and whitespace checks will not remove them.

What replacing or normalizing whitespace actually changes

Swapping one space for another changes the stored code points, not just the appearance. A no-break space and an ordinary space are different characters to every comparison, search and hash, even when they render at the same width. Visual effects work differently. CSS letter-spacing, word-spacing and the white-space property change how text is displayed but leave the characters alone, so copying the text gives back the original code points.

Unicode normalization is the standard way to fold space variants. Under the compatibility forms NFKC and NFKD, U+00A0, U+2002 through U+200A, U+202F, U+205F and U+3000 all map to U+0020, and so do U+2000 and U+2001 through their canonical equivalents U+2002 and U+2003. Normalization also removes the no-break behavior, because the result is an ordinary breakable space. It does not affect U+1680, the line separators, the control characters or the zero-width Cf characters. Those need explicit handling.

import unicodedata

s = '10\u00a0km'
print(unicodedata.normalize('NFKC', s) == '10 km')  # True

Removing zero-width characters is not always harmless. U+200C and U+200D are needed for correct rendering in several scripts, and U+200D joins many multi-character emoji. Deleting them can split an emoji into its parts or change how a word displays. Strip them from identifiers and matching keys, but leave them in display text unless you know they were inserted by mistake.

How to identify an unknown space character

Looking at the text will not tell you which space it contains. Widths depend on the font, and several characters render identically. These are reliable clues:

  • A gap that will not wrap at a line end suggests U+00A0, U+2007, U+202F or U+2060.
  • A line that wraps where no gap is visible suggests U+200B.
  • Cursor movement that needs an extra keypress where nothing is visible indicates a zero-width character.
  • Text that looks identical but fails an equality check or search usually contains a non-ASCII space or an invisible format character.
  • A file whose first line misbehaves often starts with U+FEFF.
  • A "blank" username or message that survives trimming probably uses U+3164, U+2800 or a similar non-whitespace character.

To confirm, list the code points. When looking for a ready-made tool, search for terms like "Unicode code point inspector", "Unicode character identifier" or "show invisible characters". Many code editors can render whitespace or highlight unusual Unicode characters. Look in your editor's settings under whitespace rendering or Unicode highlighting, because the option names differ between editors. If you prefer not to paste text into a web service, a few lines of code using only a standard library will do the same job locally.

import unicodedata

def inspect(text):
    for ch in text:
        name = unicodedata.name(ch, '<no name>')
        cat = unicodedata.category(ch)
        print(f'U+{ord(ch):04X}  {cat}  {name}  isspace={ch.isspace()}')

inspect('10\u00a0km\u200b')

According to Python's Unicode database, that call reports the following:

U+0031  Nd  DIGIT ONE  isspace=False
U+0030  Nd  DIGIT ZERO  isspace=False
U+00A0  Zs  NO-BREAK SPACE  isspace=True
U+006B  Ll  LATIN SMALL LETTER K  isspace=False
U+006D  Ll  LATIN SMALL LETTER M  isspace=False
U+200B  Cf  ZERO WIDTH SPACE  isspace=False

The default argument to unicodedata.name matters. Control characters such as tab and line feed have no character name in that database, and without a default the call raises an error. JavaScript has no built-in name lookup, but it can list code points and show how its own whitespace class classifies each one:

const inspect = (text) =>
  [...text].map((ch) => {
    const hex = ch.codePointAt(0).toString(16).toUpperCase().padStart(4, '0');
    return `U+${hex}${/\s/u.test(ch) ? '  JS whitespace' : ''}`;
  });

console.log(inspect('a\u00a0b\ufeff'));
// [ 'U+0061', 'U+00A0  JS whitespace', 'U+0062', 'U+FEFF  JS whitespace' ]

Spreading the string with [...text] iterates by code point instead of UTF-16 unit, so characters outside the Basic Multilingual Plane are reported once rather than as two surrogate halves. Multi-code-point sequences such as emoji joined with U+200D will still appear as several entries, which is useful when you are looking for hidden joiners.

Why different tools disagree about what is whitespace

Every environment uses its own definition, so an inspection result only applies to the definition you checked against. These differences are documented and commonly cause surprises:

  • JavaScript's \s and String.prototype.trim include U+FEFF, even though Unicode does not give it the White_Space property.
  • Python's str.isspace returns True for U+00A0 and False for U+200B, and str.split() with no argument splits on Unicode whitespace, including no-break spaces.
  • Java's Character.isWhitespace deliberately excludes the non-breaking spaces U+00A0, U+2007 and U+202F. Java's regex \s matches only ASCII whitespace unless Unicode character classes are enabled.
  • In HTML, normal white-space handling collapses runs of ASCII whitespace, but U+00A0 is preserved. That is why the   entity is used to force visible spacing.
  • Spreadsheet trim functions often remove only the ordinary space, so values copied from web pages may keep a trailing no-break space that breaks lookups.

When you clean data, choose the definition on purpose and write it down. Matching an explicit character class, or normalizing to NFKC and then removing a named list of format characters, gives results you can reproduce. Relying on whichever trim function is closest to hand does not.

Copying, sharing, search and accessibility

Copy and paste normally carries the exact code points along, which is why a stray no-break space or zero width space moves with the text from one application to the next. Some destinations normalize, collapse or strip whitespace on input, and others keep everything. This review did not test specific platforms, so it makes no claims about any named site, messaging app or editor. If a particular destination matters, paste a sample containing the characters you care about, then run the copied result back through an inspector to see what survived.

Search is the most common practical failure. A query typed with an ordinary space may not match text stored with U+00A0 or U+202F, and a zero width space inside a word can stop the word from matching at all, depending on how the search system normalizes input. Reading order is not affected by space characters themselves. The bidirectional formatting characters, which are a separate group of invisible Cf characters, can reorder displayed text, and an inspector will reveal them just as it reveals spaces.

Screen readers generally do not announce ordinary spaces. How they handle zero-width or unusual characters inside words varies by product, settings and language, and no assistive-technology testing was available for this review. The cautious working rule is to keep invisible characters out of words, names and labels that people need to hear correctly, and to provide plain text with ordinary spaces wherever accessibility matters.

Gaps in this review

The character properties, normalization mappings and language behaviors above come from Unicode and language documentation. This review includes no measured rendering widths, no tests on named destinations and no screen-reader results. Rendered width depends on the font, and platform handling can change over time. Treat any single successful display as evidence about that environment only.