Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

Python: Strip Non-ASCII Characters Without Transliteration

Use ASCII encoding with the ignore handler to delete unencodable characters, then decode the bytes back to a string. For controlled mappings, use str.translate().
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
text = "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.

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.

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():

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.