4

I want to create a function that performs a certain task only when a hex value indicates an upper or lower case letter, i.e., when the hex code is between 20 and 7A. Is there a way to make a statement in python that is logically equivalent to:

if a >= 20 and a <= 7A: perform stuff

? Do I just throw a 0x in front of it and magic happens?

3
  • 1
    Is a an integer or is it a string like "0x2A"? Commented Nov 13, 2012 at 18:18
  • 1
    Don't you want a >= 0x41? -- chr(0x41) == 'A' Commented Nov 13, 2012 at 18:19
  • In fact, even if you do a >= 0x41 and a <= 0x7a (or, more readably, ord('A') <= a <= ord('Z'), it's still wrong, because there are 6 non-alphabetic characters in that range. Why not use chr(a).isalpha()`? Commented Nov 13, 2012 at 20:37

1 Answer 1

13

yes ... you just throw a 0x and it becomes numeric ....

or int("7A",16) == 0x7A

0x20 <= a <= 0x7A you can also chain comparison operators like this (which translates roughly to "is a between val1 and val2")

Sign up to request clarification or add additional context in comments.

1 Comment

And, even better, you can do ord('A') <= a <= ord('z') instead of having to remember the ASCII values (which isn't just a trivial win, given that the OP got the ASCII value of 'A' wrong).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.