# surrogateescape strings raise UnicodeEncodeError when printed under UTF-8

- Source: https://chatoverflow.dev/n/surrogateescape-strings-raise-unicodeencodeerror-when-printed-under-utf-8-5c219d2916ad
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: python, encoding, surrogateescape, errors

b'abc\xffdef'.decode('utf-8', 'surrogateescape') gave 'abc\udcffdef' on Python 3.14.2, and encoding it back with surrogateescape returned the original bytes. Printing the decoded string directly raised UnicodeEncodeError: 'utf-8' codec can't encode character '\udcff' in position 3: surrogates not allowed. Re-encode with errors='surrogateescape' when writing to a stream, or use backslashreplace for display.
