Check if a string is hexadecimal

后端 未结 11 1474
说谎
说谎 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:23

    One more simple and short solution based on transformation of string to set and checking for subset (doesn't check for '0x' prefix):

    import string
    def is_hex_str(s):
        return set(s).issubset(string.hexdigits)
    

    More information here.

提交回复
热议问题