Check a String is Hexadecimal in Python

  • Post category:String

In this article, you will see how to check a string is hexadecimal in Python. A hexadecimal string is a string that contains only digits 0-9 and letters A-F. To check if a string is a hexadecimal string, you can use string.hexdigits().

Step 1: Import the necessary module

import string

Step 2: Create a string

str = "1234567890abcdef"
print("str = ", str)
print("str type = ", type(str))

Step 3: Check if the string is hexadecimal

if all(c in string.hexdigits for c in str):
    print("Not Hexadecimal")


str =  1234567890abcdef
str type =  <class 'str'>

Free resources to learn advanced skills: AiHints and CodeAllow