Python isascii() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

isascii() answers one question: does the string contain only ASCII characters? ASCII covers the English letters, the digits, common punctuation, spaces, and control characters, which are the code points 0 to 127.

It returns True or False. An accented letter such as the one in "caf\u00e9", an emoji, or a letter from another alphabet makes it False. An empty string returns True, because it holds no character outside the range. The method was added in Python 3.7.

text.isascii() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Call it on any string. There are no arguments, but the parentheses are required.

python
ok = text.isascii()
CallWhat it does
"Hello 123!".isascii()Returns True. Letters, digits, a space, and punctuation are all ASCII.
"caf\u00e9".isascii()Returns False. The accented letter is outside the ASCII range.
"".isascii()Returns True. An empty string has no character that is not ASCII.

To see which characters are the problem, loop over the string and test each one. To turn text into bytes use encode().

Library declarations
python
str.isascii() -> bool
# returns: True if all characters are ASCII (or the string is empty)
# added in Python 3.7

Parameters

This method takes no arguments. It always checks the whole string.

Return Value

Return ValueDescription
boolTrue if the string is empty or every character has a code point below 128. Otherwise False.

Python Examples

Example 1 — Which Strings Are Plain ASCII?

Test an ordinary word, a word with an accent, and an empty string.

python
samples = {"plain": "Hello", "accent": "caf\u00e9", "empty": ""}

for label, text in samples.items():
    print(label, text.isascii())

How It Works

  1. Plain letters pass. "Hello" uses only ASCII characters, so the result is True.
  2. The accent fails. The escape \u00e9 is a letter outside the ASCII range, so one such character is enough to give False.
  3. Empty is ASCII. An empty string has no non-ASCII character, so isascii() returns True.

Example 2 — Accept Only Simple Usernames

Combine isascii() with isalnum() in a small function.

python
def is_simple_username(name):
    return name != "" and name.isascii() and name.isalnum()


print(is_simple_username("ada99"))
print(is_simple_username("ad\u00e9la"))
print(is_simple_username("ada 99"))
print(is_simple_username(""))

How It Works

  1. Three rules. The name must not be empty, must be ASCII, and must contain only letters and digits.
  2. The accent breaks rule two. "ad\u00e9la" is not ASCII, so the function returns False.
  3. The space breaks rule three. "ada 99" is ASCII, but isalnum() is False because of the space.
  4. Empty is rejected explicitly. Without the first test, an empty string would pass isascii(), so the function checks for it first.

Example 3 — Check Before You Encode

Pick the encoding from the check, then compare the size of the text and of the bytes.

python
text = "na\u00efve idea"

if text.isascii():
    data = text.encode("ascii")
else:
    data = text.encode("utf-8")

print(text.isascii())
print(len(text), len(data))

How It Works

  1. The test decides. The text contains one letter outside ASCII, so isascii() is False and the else branch runs.
  2. UTF-8 handles any text. encode("utf-8") turns the string into bytes without raising an error.
  3. Bytes can be longer. The text has 10 characters, but that one letter needs two bytes, so the data has 11 bytes.

Frequently Asked Questions

It returns True if every character in the string is an ASCII character, with a code point from 0 to 127, and False if even one is outside that range.
True. An empty string contains no non-ASCII character. Add a test such as text != "" if you need at least one character.
No. It only checks the code points, so "!?#" is ASCII. Use isalnum() or isalpha() to check the kind of character.
Python 3.7. In older versions you can test text.encode("ascii") inside try/except, or compare each ord(ch) with 127.
Loop over the string and keep each character whose isascii() is False, for example [ch for ch in text if not ch.isascii()].

Next: isdecimal()

True if every character is a decimal digit.

isdecimal() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful