Content · Programming · Python

Python str.isascii Method: Usage and Examples

str.isascii() reports whether every code point in the string is in the ASCII range U+0000 through U+007F. It also returns True for an empty string and for ASCII control characters. Learn with a clear example and common.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

This guide explains str.isascii Method: Usage and Examples through a focused example and practical reasoning. No prior knowledge of this specific operation is required.

Understanding the Concept


str.isascii() reports whether every code point in the string is in the ASCII range U+0000 through U+007F. It also returns True for an empty string and for ASCII control characters.

When You Would Use This


Detect whether a message contains characters outside ASCII.

Step-by-Step Example


message_samples = ["hello", "café", "123\n", ""]
for message_text in message_samples:
    print(repr(message_text), message_text.isascii())

How the Example Works


hello, the text containing a newline and the empty string all return True; café returns False. ASCII status does not imply a string is printable or safe.

Example Output


'hello' True
'café' False
'123\n' True
'' True

Try It Yourself


Try a string containing a tab and explain why it passes even though it is not visible text.

Common Beginner Mistakes


  • Assuming isascii() accepts only letters and digits.
  • Assuming the empty string returns False.
  • Using ASCII checks as a replacement for proper encoding and input validation.

Key Takeaways


  • str.isascii() reports whether every code point in the string is in the ASCII range U+0000 through U+007F.
  • hello, the text containing a newline and the empty string all return True; café returns False.
  • Avoid this mistake: Assuming isascii() accepts only letters and digits.
Share this resourceSend this page to someone who may find it useful.