"""UTF-8 test file for Python: non-ASCII strings, names and comments. Free sample from 99helpers.com/file-types/py Run: python3 sample-utf8.py If any line below looks garbled (é, ’), something read this file in the wrong encoding. Python itself always reads it as UTF-8. """ import sys import unicodedata # 1. No "# -*- coding: utf-8 -*-" line needed: UTF-8 is the default source # encoding in Python 3 (PEP 3120). Python 2 assumed ASCII. # 2. Make print() write UTF-8 even where the console or a redirect expects # another encoding (older Python versions on Windows, for example). sys.stdout.reconfigure(encoding="utf-8") GREETINGS = { "Dutch": "Goedemorgen, café-eigenaar", "German": "Grüß Gott, Straße", "French": "Ça va, Zoë ? Très bien", "Greek": "Καλημέρα", "Russian": "Здравствуйте", "Arabic": "مرحبا", "Hebrew": "שלום", "Hindi": "नमस्ते", "Japanese": "こんにちは", "Chinese": "你好", "Korean": "안녕하세요", "Symbols": "€ £ ¥ ₹ ≤ ≥ ∞ ✓", "Emoji": "👋🌍🐍", } # 3. len() counts code points; encoding to UTF-8 shows the bytes on disk. for language, text in GREETINGS.items(): size = len(text.encode("utf-8")) print(f"{language:<9} {text} ({len(text)} characters, {size} bytes)") # 4. Names can be non-ASCII too (PEP 3131), though PEP 8 asks the standard # library to stick to ASCII, and most projects do the same. café = "a variable called café" print(café) # 5. Normalisation: "é" can be one code point or "e" plus a combining accent. # They look identical but compare unequal until normalised. composed, decomposed = "\u00e9", "e\u0301" print(composed == decomposed, unicodedata.normalize("NFC", decomposed) == composed) # 6. casefold() is the right way to compare text without case: "ß" folds to "ss". print("Straße".lower(), "Straße".casefold()) print("STRASSE".casefold() == "Straße".casefold())