Sub topic 1
Why the Same Text Looks Different to a Computer
Text can look identical on screen and still compare as unequal in a program. This happens because a computer stores text as numeric values, and more than one sequence of values can represent what a reader sees as the same character or phrase.
Prerequisites
You should know basic Python strings, loops, list comprehensions, and functions. You should also know that text can be stored as bytes using an encoding such as UTF-8, and that natural-language systems often split text into tokens.
The difference between appearance and representation
A reader sees characters and words. A computer sees a sequence of code points.
A code point is a number assigned to a character by Unicode, the standard that defines characters for many writing systems. For example, the letter A has code point U+0041, and the lowercase letter a has code point U+0061. The prefix U+ means that the number is written in hexadecimal.
An encoding converts code points into bytes so that text can be stored or transmitted. UTF-8 is a common encoding. The code point and its UTF-8 bytes are related, but they are not the same thing.
A user-perceived character is one visible unit from a reader's point of view. It might consist of multiple code points. For example, é can be represented as one code point, or as the letter e followed by a combining acute accent.
import unicodedata
one_code_point = "é"
two_code_points = "e\u0301"
print(one_code_point == two_code_points) # False
print(len(one_code_point)) # 1
print(len(two_code_points)) # 2
for character in two_code_points:
print(character, unicodedata.name(character))The two strings render alike, but they contain different sequences. The first contains a precomposed é. The second contains a plain e and a combining mark. Python's len() counts code points, not necessarily the characters a person perceives.
A similar issue occurs with text copied from different systems. One source might use a normal space, while another uses a non-breaking space. Both may look like a gap between words. One source might use a straight quotation mark, while another uses a curly quotation mark. These distinctions can affect equality checks, search, tokenisation, and machine-learning features.
Why equality checks fail
Suppose a program uses text as a dictionary key or checks whether a user-entered name matches a stored name. A direct comparison checks the underlying code-point sequences:
left = "café"
right = "cafe\u0301"
print(left == right) # FalseThe comparison is technically correct because the sequences differ. It is often wrong for the application's purpose, however, because users usually expect these two spellings to match.
This problem is separate from encoding errors. If bytes are decoded with the wrong encoding, the result can contain replacement characters or corrupted text. If the bytes are decoded correctly, the resulting string can still use a different Unicode representation from another correctly decoded string.
Unicode defines normalisation methods for handling equivalent representations. Normalisation changes a string into a selected, consistent form. Later, you will use forms such as NFC and NFD to make canonically equivalent text compare consistently. The important point here is that visual equality does not guarantee code-point equality, so text-processing pipelines need an explicit policy.
Inspecting hidden differences
When a comparison behaves unexpectedly, inspect the representation rather than relying on the display:
import unicodedata
text = "cafe\u0301"
print(repr(text))
print([f"U+{ord(ch):04X}" for ch in text])
print([unicodedata.name(ch) for ch in text])repr() makes escape sequences and some invisible characters easier to notice. ord() returns the numeric code point for one character. unicodedata.name() gives the Unicode name when one exists.
What this means for natural-language processing
Natural-language processing systems often compare, count, search, and tokenise strings. Unnormalised representations can create duplicate vocabulary entries. For example, a model might treat the two forms of é as separate tokens. A search system might find one form but miss the other. A deduplication step might keep both copies.
Normalisation is therefore a deliberate preprocessing choice. It should happen at a stable boundary, such as when text enters a data pipeline, and the same policy should be applied to stored data and incoming queries. Do not silently assume that two strings that look the same have the same representation.
Exercise
Why can "é" and "e\u0301" look the same but compare as unequal in Python?
Show answer
They contain different sequences of Unicode code points. The first commonly uses one precomposed code point, while the second uses e followed by a combining acute-accent code point.