Python – Filter Strings within ASCII range

When working with text data, you may need to verify if all characters in a string fall within the ASCII range (0-127). Python provides the ord() function to get Unicode values and all() to check conditions across all elements.

Understanding ASCII Range

ASCII characters have Unicode values from 0 to 127. This includes letters (A-Z, a-z), digits (0-9), punctuation, and control characters. Characters with Unicode values 128 and above are non-ASCII.

Method 1: Using all() with ord()

The most concise approach uses all() with a generator expression ?

my_string = "Hope you are well"

print("The string is :")
print(my_string)

my_result = all(ord(c) < 128 for c in my_string)

if my_result:
    print("The string contains only ASCII characters")
else:
    print("The string contains non-ASCII characters")
The string is :
Hope you are well
The string contains only ASCII characters

Method 2: Testing with Non-ASCII Characters

Let's test with a string containing non-ASCII characters ?

test_strings = [
    "Hello World",
    "Café",
    "Python programming",
    "?????"
]

for text in test_strings:
    is_ascii = all(ord(c) < 128 for c in text)
    status = "ASCII only" if is_ascii else "Contains non-ASCII"
    print(f"'{text}' - {status}")
'Hello World' - ASCII only
'Café' - Contains non-ASCII
'Python programming' - ASCII only
'?????' - Contains non-ASCII

Method 3: Using try-except with encode()

Alternative approach using string encoding ?

def is_ascii_string(text):
    try:
        text.encode('ascii')
        return True
    except UnicodeEncodeError:
        return False

test_text = "Hello World"
print(f"'{test_text}' is ASCII: {is_ascii_string(test_text)}")

test_text2 = "Naïve"
print(f"'{test_text2}' is ASCII: {is_ascii_string(test_text2)}")
'Hello World' is ASCII: True
'Naïve' is ASCII: False

Comparison

Method Performance Readability Best For
all(ord(c) < 128) Fast High General use
encode('ascii') Slower Medium Exception handling needed

How It Works

  • The ord() function returns the Unicode code point of a character

  • ASCII characters have Unicode values 0-127

  • The all() function returns True only if all conditions are met

  • Generator expression (ord(c) < 128 for c in string) checks each character efficiently

Conclusion

Use all(ord(c) < 128 for c in string) for efficient ASCII range filtering. The ord() function provides Unicode values, while all() ensures every character meets the ASCII criteria.

Updated on: 2026-03-26T00:53:05+05:30

404 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements