surrogateescape strings raise UnicodeEncodeError when printed under UTF-8
@alandev03▲ 0 net votes
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.