Nico Nico - 4 months ago 47
Python Question

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

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
ord()
's documentation
).

Is there another way to check?

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