How do you check if a string contains only ASCII characters in Python?
Posted by PaulAnd
Last Updated: August 26, 2024
Checking for ASCII Characters in a String Using Python
In Python, a string is considered to contain only ASCII characters if all characters fall within the ASCII range, which includes characters from 0 to 127. Several methods can be employed to check if a string contains only ASCII characters. Below are some of the most common and effective approaches.
Method 1: Using the str.isascii() Method
Introduced in Python 3.7, the str.isascii() method can be utilized to determine if all characters in the string are ASCII. This is the simplest and most efficient method.
def is_ascii(s: str) -> bool:
    return s.isascii()

# Example usage
string = "Hello, World!"
result = is_ascii(string)
print(f"Is the string ASCII? {result}")
Method 2: Using the all() Function with ord()
The ord() function returns the Unicode code point for a given character. By combining all() with a generator expression, you can check each character in the string.
def is_ascii(s: str) -> bool:
    return all(ord(char) < 128 for char in s)

# Example usage
string = "Hello, World!"
result = is_ascii(string)
print(f"Is the string ASCII? {result}")
Method 3: Using Regular Expressions
Regular expressions provide a robust method for pattern matching. The following example uses the re module to validate ASCII characters.
import re

def is_ascii(s: str) -> bool:
    return bool(re.match(r'^[\x00-\x7F]*$', s))

# Example usage
string = "Hello, World!"
result = is_ascii(string)
print(f"Is the string ASCII? {result}")
Method 4: Encoding and Decoding
Another approach involves attempting to encode the string in ASCII format and catching any UnicodeEncodeError.
def is_ascii(s: str) -> bool:
    try:
        s.encode('ascii')
        return True
    except UnicodeEncodeError:
        return False

# Example usage
string = "Hello, World!"
result = is_ascii(string)
print(f"Is the string ASCII? {result}")
Conclusion
Checking if a string contains only ASCII characters can be accomplished in multiple ways in Python. The method chosen may depend on specific use cases and Python version constraints. For most scenarios, the str.isascii() method provides a clear and efficient solution. For older versions of Python, using ord() or regular expressions are reliable alternatives.