Algorithmscase normalization

Case Normalization

TT
Testlaa Team
May 14, 20261 min read

Case normalization picks a canonical representation so 'Email' and 'EMAIL' collide in a hash map. Common choices: all lowercase, all uppercase, or Unicode casefold for aggressive matching.

Why this shows up in the real world

Email login normalizes addresses to lowercase. Search indexes fold case so queries find mixed-case documents. Legal deduping sometimes applies casefold to catch stylized spellings.

Core idea (explained for students)

Python: s.lower() for simple ASCII-centric tasks; s.casefold() when you must treat more Unicode equivalents (German ß casefolds to ss). Store normalized keys, but keep originals if display matters.

Try this in Python

def login_key(email: str) -> str:
    return email.strip().casefold()


print(login_key("  User@MAIL.com "))

Common mistakes

  • Using upper() for keys when Turkish locale rules could surprise you—tests often stick to ASCII.
  • Normalizing too early and losing original casing users typed.

Key takeaways

  • Default recipe: key = text.strip().casefold() for user-generated identifiers.
  • Document which normalization you chose in API specs.

Tags:

StringsPythonStudents