Check if a string is hexadecimal

后端 未结 11 1475
说谎
说谎 2020-12-13 06:14

I know the easiest way is using a regular expression, but I wonder if there are other ways to do this check.

Why do I need this? I am writing a Python script that re

11条回答
  •  旧巷少年郎
    2020-12-13 06:29

    Another option:

    def is_hex(s):
        hex_digits = set("0123456789abcdef")
        for char in s:
            if not (char in hex_digits):
                return False
        return True
    

提交回复
热议问题