I want to I check whether a string is in ASCII or not.
I am aware of
ord(), however when I try
ord('é'), I have
TypeError: ord() expected a character, but string of length 2 found. I understood it is caused by the way I built Python (as explained in
Is there another way to check?
def is_ascii(s): return all(ord(c) < 128 for c in s)
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"