Skip to content

UnicodeEncodeError: 'ascii' codec can't encode character

Error message

                'ascii' codec can't encode character '\u2603' in position 0
              

Quick summary

You tried to encode a string to an encoding that can't represent some of its characters.

Why this happens

  • Encoding a Unicode string to ASCII (which can't handle non-ASCII chars)

  • Writing Unicode text to a file opened with a limited encoding

  • Printing Unicode characters to a console that doesn't support them

Minimal example

✗ Broken code
text = 'Hello \u2603'  # Snowman character
encoded = text.encode('ascii')  # UnicodeEncodeError
✓ Fixed code
text = 'Hello \u2603'
encoded = text.encode('utf-8')  # Works — UTF-8 supports all Unicode
print(encoded)

ASCII can't encode the snowman character. Using UTF-8 encoding instead handles all Unicode characters.

How to diagnose

  • Check which character is causing the error (shown in the message)

  • Verify the target encoding supports the characters in your string

  • Use errors='replace' or errors='ignore' to handle unencodable chars

How to fix

  • Use UTF-8 encoding instead of ASCII: encode('utf-8')

  • Use errors='replace' or errors='ignore' for lossy encoding

  • Open files with encoding='utf-8' to support all characters

How to prevent

  • Always use UTF-8 for text storage and transmission

  • Specify encoding='utf-8' when opening files for writing

Related Resources

Related Glossary

Related Lessons

← Back to language