encodings Module¶
The encodings module provides Python's standard character encoding implementations, including UTF-8, ASCII, Latin-1, and many others.
Complexity Reference¶
| Operation | Time | Space | Notes |
|---|---|---|---|
| Get encoding | O(1) | O(1) | Cache lookup |
| Encode/decode | O(n) | O(n) | n = string length |
Character Encodings¶
Using Standard Encodings¶
# UTF-8 encoding - O(n)
text = "Hello, 世界"
utf8 = text.encode('utf-8')
decoded = utf8.decode('utf-8')
# ASCII encoding - O(n)
ascii_text = "Hello World"
ascii_bytes = ascii_text.encode('ascii')
# Latin-1 encoding - O(n)
latin1_text = "Café"
latin1_bytes = latin1_text.encode('latin-1')
# Handle errors - O(n)
bad_text = "Bad café"
try:
# Raises UnicodeEncodeError
ascii_bytes = bad_text.encode('ascii')
except UnicodeEncodeError:
# Use error handling
ascii_bytes = bad_text.encode('ascii', errors='ignore')
ascii_bytes = bad_text.encode('ascii', errors='replace')