pythonord

The right way to check if a string has hebrew chars


The Hebrew language has unicode representation between 1424 and 1514 (or hex 0590 to 05EA).

I'm looking for the right, most efficient and most pythonic way to achieve this.

First I came up with this:

for c in s:
    if ord(c) >= 1424 and ord(c) <= 1514:
        return True
return False

Then I came with a more elegent implementation:

return any(map(lambda c: (ord(c) >= 1424 and ord(c) <= 1514), s))

And maybe:

return any([(ord(c) >= 1424 and ord(c) <= 1514) for c in s])

Which of these are the best? Or i should do it differently?


Solution

  • You could do:

    # Python 3.
    return any("\u0590" <= c <= "\u05EA" for c in s)
    # Python 2.
    return any(u"\u0590" <= c <= u"\u05EA" for c in s)