How to Check if a String is ASCII in Python

Say you’re sending customer names from a Chicago shop to a label printer that only takes ASCII. To check if a string is ASCII in Python before it goes out, call name.isascii(). It returns True only if every character is ASCII (0-127).

In this tutorial, we’ll walk through four ways to check if a string is ASCII in Python, then pin down the exact characters that fail. I ran everything on Python 3.12.5.

The sample customer names we’ll check for ASCII

Every example imports this list from orders.py:

# Customer names from a Chicago shop's 2026 order export
names = ["Emily Carter", "José Ramírez", "Ava’s Bakery",
         "Order #1042 $19.99", "", "Ryan\tMitchell"]

Two names have accents, one has a curly apostrophe from Word, one is empty, one hides a tab.

Method 1: Check for ASCII with str.isascii() (Recommended)

On Python 3.7+, this built-in is the fastest option. Here’s the code on our six names:

from orders import names

for name in names:
    print(f"{name!r:24} isascii: {name.isascii()}")

Output:

'Emily Carter'           isascii: True
'José Ramírez'           isascii: False
'Ava’s Bakery'           isascii: False
'Order #1042 $19.99'     isascii: True
''                       isascii: True
'Ryan\tMitchell'         isascii: True
VS Code output of Python str.isascii() returning True or False for six customer names
Only the two accented names and the curly apostrophe return False; the empty string and the tab pass.

How does this code work?

isascii() stops at the first character above 127. “Ava’s Bakery” fails on the curly ’, not the straight ' key.

Don’t confuse it with the built-in Python ascii() function, which escapes text instead of testing it.

Method 2: Check for ASCII with encode() and UnicodeEncodeError

This older way still earns its spot: the exception tells you where the first bad character sits. Same six strings:

from orders import names

def is_ascii(text):
    try:
        text.encode("ascii")
    except UnicodeEncodeError as err:
        bad = text[err.start]
        print(f"{text!r}: {bad!r} at index {err.start}")
        return False
    return True

print([is_ascii(n) for n in names])

Output:

'José Ramírez': 'é' at index 3
'Ava’s Bakery': '’' at index 3
[True, False, False, True, True, True]
VS Code output of Python encode('ascii') raising UnicodeEncodeError and showing the first non-ASCII character and its index
Each failing name reports its first non-ASCII character and index; the final list matches isascii().

How does this code work?

encode("ascii") raises UnicodeEncodeError on the first unmappable character, and err.start is its index.

It’s slower on long text, since it builds bytes. Encoding anyway? See converting a string to UTF-8 in Python.

Method 3: Find the non-ASCII characters with ord()

Need every offender, not just a yes or no? Loop with ord(). Same list again:

from orders import names

for name in names:
    bad = [(ch, ord(ch)) for ch in name if ord(ch) > 127]
    ok = all(ord(ch) < 128 for ch in name)
    print(f"{name!r:24} {ok!s:5} {bad}")

Output:

'Emily Carter'           True  []
'José Ramírez'           False [('é', 233), ('í', 237)]
'Ava’s Bakery'           False [('’', 8217)]
'Order #1042 $19.99'     True  []
''                       True  []
'Ryan\tMitchell'         True  []
VS Code output of Python ord() listing each non-ASCII character and its code point for every customer name
José Ramírez shows é (233) and í (237); the curly apostrophe shows code point 8217.

How does this code work?

Python ord() returns a code point. all() gives the True/False, and the comprehension collects each character above 127. That 8217 means text pasted from Word.

Method 4: Check a string for ASCII with a regex

A regex fits when you already validate fields with patterns; for the basics, see regular expressions in Python.

from orders import names

import re

ASCII_ONLY = re.compile(r"[\x00-\x7F]*")
for name in names:
    ok = ASCII_ONLY.fullmatch(name) is not None
    print(f"{name!r:24} {ok!s:5} {re.findall(r'[^\x00-\x7F]', name)}")

Output:

'Emily Carter'           True  []
'José Ramírez'           False ['é', 'í']
'Ava’s Bakery'           False ['’']
'Order #1042 $19.99'     True  []
''                       True  []
'Ryan\tMitchell'         True  []
VS Code output of a Python regex fullmatch on [\x00-\x7F]* checking customer names for ASCII and listing the non-ASCII characters
fullmatch() passes the same four names, and findall() with the negated class lists the non-ASCII characters.

How does this code work?

[\x00-\x7F] matches any ASCII character, fullmatch() needs the whole string to match, and * lets the empty string pass. The negated class in findall() returns the misfits.

Important: plain search() without ^...$ passes any string containing one ASCII character.

Which ASCII check method should you use?

MethodBest forNote
s.isascii()Fast yes/no checkPython 3.7+
s.encode("ascii")First bad indexWorks on older Python
ord() loopListing every offenderSlowest on big text
re.fullmatch()Pattern validationAnchor the pattern

I’d use isascii() for the check and the ord() loop only for a fix-up report. To clean failing rows, remove non-ASCII characters in Python.

Things to keep in mind when checking strings for ASCII

  • Empty strings pass. Use if name and name.isascii().
  • ASCII isn’t printable. Tabs and NUL pass isascii(); pair it with isprintable().
  • Curly quotes fail. Replace ’ with ' first.

Working with text encodings a lot? These guides pair well with this one:

Frequently asked questions

How do I check if a string is ASCII in Python?

Call s.isascii(). It’s True when every code point is 127 or lower.

Which Python version added str.isascii()?

Python 3.7. Before that, catch UnicodeEncodeError from encode().

Does isascii() return True for an empty string?

Yes. "".isascii() is True, so test blanks separately.

How do I find which characters are not ASCII?

Use [c for c in s if ord(c) > 127] or re.findall(r"[^\x00-\x7F]", s).

Can I check bytes for ASCII?

Yes. bytes.isascii() is True when every byte is 0x7F or lower.

Where is str.isascii() documented?

See str.isascii() in the Python docs.