str.isascii()

Check that a string contains only ASCII characters — the odd one out of the is* family, since it returns True on empty.

String methodPython 3.7+Live demo
Common call
if user_input.isascii():
Returns
True or False
Replaces
a manual `all(ord(c) < 128 for c in s)` loop
Watch out
EMPTY STRING RETURNS TRUE — the entire is* family says False on empty EXCEPT this one
str.isascii()
bool

Demo

Live evaluation
Try:
Inputs
stringstrthe string to test
Output
'hello world'.isascii()
True

isascii() returns True only when every character has a Unicode codepoint below 128 — the ASCII range (0..127). Digits, letters A-Z / a-z, and standard punctuation all pass. Accented letters (é, ñ), Cyrillic (П), CJK (漢), and emoji all fail. THE EXCEPTION worth remembering: empty string returns True. Every other is* method returns False on empty.

Common patterns

Validate ASCII-only input
Use before writing to an ASCII-only field (some legacy databases, some protocols).
if not username.isascii():
    raise ValueError("username must be ASCII")
Combine with isalnum for &quot;ASCII alphanumeric&quot;
A stricter version of isalnum that excludes non-Latin letters and digits.
if s.isascii() and s.isalnum():
    ...   # ASCII letters and digits only
Detect non-ASCII content for encoding decisions
Fast path: skip charset detection or encoding conversion if the whole string is ASCII.
if payload.isascii():
    write_plain(payload)
else:
    write_utf8(payload)

Examples

1. Plain ASCII
"hello".isascii()
Returns
True
2. Digits and punct
"a-b_c.d".isascii()
Returns
True
3. Accented letter
"café".isascii()
Returns
False
4. Emoji
"hi 😀".isascii()
Returns
False
5. Cyrillic
"Привет".isascii()
Returns
False
6. Empty is TRUE
"".isascii()
Returns
True # the odd one out

Pitfalls

1. Empty string returns TRUE — the exception to the family
Every other is* method (isalpha, isdigit, isalnum, isspace, isupper, islower, ...) returns False on empty. isascii is the exception: it returns True. The reason is that all zero characters trivially satisfy &quot;every character is ASCII&quot;.
Assumed False
"".isascii()
True # UNLIKE the rest of is*
Guard if you need non-empty
s and s.isascii()
True only for non-empty ASCII
2. ASCII includes control characters
Codepoints 0..31 (control chars like NUL, TAB, LF, ESC) and 127 (DEL) are all ASCII. isascii returns True on strings that contain these — legitimate but unlikely to be what a validator wants.
Contains control char
"hello\x00world".isascii()
True # NUL is ASCII
Combine with isprintable
s.isascii() and s.isprintable()
False on NUL
3. NOT the same as &quot;in the Latin alphabet&quot;
isascii tests the CODEPOINT range, not whether characters are Latin letters. Punctuation, digits, spaces, and control characters all pass; anything above U+007F fails.
Assumed alphabet
"12345".isascii()
True # digits are ASCII
Combine with isalpha
s.isascii() and s.isalpha()
True only for ASCII letters
4. Some visually similar characters are NOT ASCII
The look-alike attack surface: fancy quotes (&ldquo;&rdquo;), en/em dashes (– —), non-breaking space (U+00A0), fullwidth digits (123). None are ASCII. Input that looks ASCII may not be.
Fancy quote fails
"hello &ldquo;world&rdquo;".isascii()
False # curly quotes are not ASCII
Normalize first
s = s.replace("\u201c", chr(0x22)).replace("\u201d", chr(0x22))
s.isascii()
True after normalization

When to use

Use it
  • Fast pre-check before ASCII-only encoding or storage
  • Detecting non-ASCII content to trigger UTF-8 handling
  • Validating usernames or IDs in ASCII-only contexts
  • Combining with isprintable / isalnum for stricter checks
Reach for something else
  • Case where empty must NOT count → guard with `s and`
  • Excluding control characters → combine with isprintable
  • ASCII letters only → combine with isalpha
  • Rich internationalization support — ASCII-only is usually the wrong call in 2024+

Notes

Complexity
O(n) — one linear scan
Return
bool — True or False (True on empty)
CPython impl
Objects/unicodeobject.c :: unicode_isascii
Memory
No allocation
Thread-safe
Yes — strings are immutable

FAQ

isascii tests &quot;every character has codepoint &lt; 128&quot;. For the empty string, this is vacuously true — no character can violate the rule. The rest of the is* family also requires &quot;at least one character&quot;, but isascii does not.

History

3.7
isascii() introduced — added to str, bytes, and bytearray at the same time.