Python Reference Library →

Python String isascii() Method – Check for ASCII Characters

Let’s look at string isascii() method – check for ascii characters

  1. Notice

    isascii() tests whether every character is ASCII. An empty string also satisfies that condition.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'tea'
result = text.isascii()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea'Object 2 · boolTrue
This step

isascii() tests whether every character is ASCII. An empty string also satisfies that condition.

Picture string isascii() method – check for ascii characters

isascii() tests whether every character is ASCII. An empty string also satisfies that condition.

Image brief

Alt text
A diagram for Python String isascii() Method – Check for ASCII Characters. Instructions: text = 'tea' result = text.isascii() print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: 'tea'; object 2: True
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String isascii() Method – Check for ASCII Characters". Teaching focus: isascii() tests whether every character is ASCII. An empty string also satisfies that condition. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'tea' result = text.isascii() print(result) | v DISPLAYED RESULT True FINAL BINDINGS text --> object 1 (str) result --> object 2 (bool) object 1: 'tea' object 2: True Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String isascii() Method – Check for ASCII Characters. Instructions: text = 'tea' result = text.isascii() print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: 'tea'; object 2: True

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'tea'
result = text.isascii()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea'Object 2 · boolTrue
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
True

What do you expect this program to print?

What does this example display?

text = 'tea'
result = text.isascii()
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

True

Follow the printed values in order. The output is: True

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

isascii() tests whether every character is ASCII. An empty string also satisfies that condition.

isascii() tests whether every character is ASCII. An empty string also satisfies that condition.

More examples and reference

Python String isascii() Method

The isascii() method in Python checks whether all the characters in a string are ASCII characters (i.e., in the range 0 to 127).

It returns True if all characters are ASCII, otherwise False. This method is available from Python 3.7 and later.

Syntax

string.isascii()

Parameters:

  • No parameters are required.

Returns:

  • True – If all characters in the string are ASCII.
  • False – If at least one character is not an ASCII character.

Example 1: Simple ASCII String

text = "Hello123"
print(text.isascii())
True

Example 2: String with Unicode Characters

text = "Café"
print(text.isascii())
False

The character é is not an ASCII character.

Example 3: Empty String

text = ""
print(text.isascii())
True

An empty string is considered ASCII.

Use Cases

  • Validating user input for systems that only support ASCII
  • Pre-processing text before encoding or sending to legacy systems
  • Detecting presence of special or non-English characters

Common Mistakes

  • Assuming isascii() checks if a string contains only letters or digits. It doesn’t—it allows symbols, punctuation, and whitespace too, as long as they’re ASCII.
  • Using it in Python versions earlier than 3.7 will raise an AttributeError.

Interview Tip

If you’re asked to filter out non-ASCII characters from a string, isascii() can help quickly detect such cases or be used in list comprehensions to clean strings.

Summary

  • string.isascii() checks if all characters are in the ASCII range.
  • Returns True for ASCII-only strings, False otherwise.
  • Empty strings return True.
  • Available in Python 3.7+

Practice Problem

Write a program that checks if user input contains only ASCII characters.

text = input("Enter some text: ")
if text.isascii():
    print("All characters are ASCII.")
else:
    print("Non-ASCII characters found!")

What to remember

Connect what you noticed in the example with the idea below.

Remember

isascii() tests whether every character is ASCII. An empty string also satisfies that condition.

Watch out for this idea

isascii() returns False for the empty string.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →