UnicodeEncodeError

Built-in ExceptionPython 2.0+Intermediate

Raised when encoding a string to bytes fails

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
# Triggering and catching UnicodeEncodeError
try:
    "\udce9".encode("ascii")
except UnicodeEncodeError as e:
    print(f"Caught UnicodeEncodeError: {e}")
    print(f"Type: {type(e).__name__}")

UnicodeEncodeError is raised when raised when encoding a string to bytes fails. Always catch specific exceptions rather than bare except clauses.

Try in Playground

Tags

exceptionerror-handlingcore