Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string in Python is in ASCII?

People also ask

How do I check if a string contains only ASCII characters?

str , bytes , and bytearray gained support for the new isascii() method, which can be used to test if a string or bytes contain only the ASCII characters.

How do I check if a string is Unicode or ASCII?

How to tell if an object is a unicode string or a byte string. You can use type or isinstance . In Python 2, str is just a sequence of bytes. Python doesn't know what its encoding is.

How do you find the ASCII value of a string in Python?

Use the for Loop Along With the ord() Function to Get ASCII of a String in Python. We can use the for loop and the ord() function to get the ASCII value of the string. The ord() function returns the Unicode of the passed string. It accepts 1 as the length of the string.

How do you find non ASCII characters in Python?

The isascii() function returns a boolean value where True indicates that the string contains all ASCII characters and False indicates that the string contains some non-ASCII characters.


I think you are not asking the right question--

A string in python has no property corresponding to 'ascii', utf-8, or any other encoding. The source of your string (whether you read it from a file, input from a keyboard, etc.) may have encoded a unicode string in ascii to produce your string, but that's where you need to go for an answer.

Perhaps the question you can ask is: "Is this string the result of encoding a unicode string in ascii?" -- This you can answer by trying:

try:
    mystring.decode('ascii')
except UnicodeDecodeError:
    print "it was not a ascii-encoded unicode string"
else:
    print "It may have been an ascii-encoded unicode string"

def is_ascii(s):
    return all(ord(c) < 128 for c in s)

In Python 3, we can encode the string as UTF-8, then check whether the length stays the same. If so, then the original string is ASCII.

def isascii(s):
    """Check if the characters in string s are in ASCII, U+0-U+7F."""
    return len(s) == len(s.encode())

To check, pass the test string:

>>> isascii("♥O◘♦♥O◘♦")
False
>>> isascii("Python")
True

New in Python 3.7 (bpo32677)

No more tiresome/inefficient ascii checks on strings, new built-in str/bytes/bytearray method - .isascii() will check if the strings is ascii.

print("is this ascii?".isascii())
# True