How to check if a string in Python is in ASCII?
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"
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