Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 characterASCII characters have Unicode values 0-127
The
all()function returnsTrueonly if all conditions are metGenerator 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.
