Vincent Marchetti has the right idea, but str.decode
has been deprecated in Python 3. In Python 3 you can make the same test with str.encode
:
try:
mystring.encode('ascii')
except UnicodeEncodeError:
pass # string is not ascii
else:
pass # string is ascii
Note the exception you want to catch has also changed from UnicodeDecodeError
to UnicodeEncodeError
.