October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk1 min

How to Check if a String Is ASCII in Python

Use str.isascii() to test whether a Python string contains only ASCII characters. For Python versions before 3.7, use strict ASCII encoding and handle UnicodeEncodeError.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Python’s built-in str.isascii() method: text.isascii() returns True when the string is empty or every character is in the ASCII range U+0000 through U+007F. It has been available since Python 3.7.

Check a string with str.isascii()

For Python 3.7 and later, call the method directly on the string:

text = "Hello, world!"
print(text.isascii())  # True

text = "café"
print(text.isascii())  # False

The method checks every character, not whether the text looks like an English word. A character outside U+0000–U+007F makes the result False. The empty string returns True, because it contains no non-ASCII characters. See the Python 3.13 documentation for str.isascii().

Use a fallback on Python versions before 3.7

str.isascii() was added in Python 3.7. If your code must run on an earlier version, try strict ASCII encoding and catch UnicodeEncodeError:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
def is_ascii(text):
    try:
        text.encode("ascii")
    except UnicodeEncodeError:
        return False
    return True

This returns True for an empty string and False if any character cannot be encoded as ASCII. Strict encoding leaves the input unchanged and makes an unencodable character an explicit error; modes such as errors="ignore" or errors="replace" can discard or alter characters, so they do not test whether the original string was entirely ASCII. Python’s Unicode HOWTO explains the distinction between Unicode strings and encoding them as bytes.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

ASCII does not mean printable or alphanumeric

ASCII includes control characters as well as letters, digits, punctuation, and whitespace. Therefore, isascii() answers only whether all code points are within the ASCII range. If your requirement is that text be printable or contain only letters and digits, apply a separate check for that requirement.

Which approach should you use?

Approach Python compatibility Best for
text.isascii() Python 3.7 and later Clear, direct checks on supported runtimes
Strict text.encode("ascii") with UnicodeEncodeError handling Older Python versions Code that must support runtimes predating Python 3.7

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.

Leave a Reply

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

Free tools Windows power users keep installed

One-click scans. No signup required.

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
PC Slower Than It Used to Be?Free scan - under a minute
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.