To keep only characters Python can encode as ASCII, encode the string with the ignore error handler, then decode the result back to a string:
text = "café — 東京"
clean = text.encode("ascii", "ignore").decode("ascii")
print(clean) # caf
This deletes the accented é, em dash, and Japanese characters. It does not convert them to ASCII equivalents.
As an Amazon Associate I earn from qualifying purchases.
What the encode-and-decode method does
Python strings are Unicode, so they can contain characters outside ASCII. ASCII encoding cannot represent those characters. With "ignore", Python drops characters that cannot be encoded:
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemstext = "café — 東京"
encoded = text.encode("ascii", "ignore")
clean = encoded.decode("ascii")
print(clean) # caf
str.encode() returns a bytes object; decoding those bytes as ASCII returns a Python str. The Python Software Foundation explains that str.encode() returns a bytes representation of a Unicode string in its Unicode HOWTO.
#1 Best Overall
Deletion is not transliteration
"ignore" removes characters ASCII cannot represent. It does not turn é into e, 東京 into Tokyo, or ß into ss. If you need readable equivalents, use a transliteration library or define explicit mappings appropriate to the characters and language. Do not use the ignore handler when losing those characters is unacceptable.
Remove non-ASCII characters with a translation table
A translation table can delete selected characters directly from a string. In Python’s character-map translation, a character mapped to None is removed; characters not in the map remain unchanged.
Rank #2
text = "café — 東京"
remove_non_ascii = {ord(ch): None for ch in text if not ch.isascii()}
clean = text.translate(remove_non_ascii)
print(clean) # caf
For a reusable helper that removes every non-ASCII character, filter the string with str.isascii():
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →def remove_non_ascii(text: str) -> str:
return "".join(ch for ch in text if ch.isascii())
These examples express the same basic keep-or-delete rule. Choose based on whether you prefer the compact encode/decode idiom or a direct string transformation; no performance ranking is established here.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Choose how encoding errors should appear
Ignoring errors silently loses information. Python’s encoding error handlers offer other treatments for characters that ASCII cannot encode:
- Delete:
text.encode("ascii", "ignore").decode("ascii")omits unencodable characters. - Replace:
text.encode("ascii", "replace")produces bytes with?in place of encoding failures. - Escape:
text.encode("ascii", "backslashreplace")represents unencodable characters with backslash escapes. - Use numeric character references:
text.encode("ascii", "xmlcharrefreplace")represents them as numeric references. - Map deliberately: use
str.translate()when selected characters need specific substitutions or deletion.
The encode examples return bytes. Decode the bytes if you need a Python string afterward; the translation and filtering examples already return strings.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.




