Python string | hex_number



In Python3 string.hexdigits — it is a pre-initialized string used as a string constant. In Python, string.hexdigits will output the hexadecimal letters “0123456789abcdefABCDEF”.

Syntax: string.hexdigits

Parameters: Doesn`t take any parameter, since it`s not a function.

Returns: Return all hexadecimal digit letters.

Note. Make sure to import the string library function to use string.hexdigits.

Code # 1:

# import string library function

import string 

 
# Store the value as a result of a variable

result = string.hexdigits 

  
# Print value

print (result) 

Output:

 0123456789abcdefABCDEF 

Code # 2: This code checks if the string input contains only hexadecimal digits

# import string library function

import string 

 
# The function checks if the input string is
# has only hexadecimal numbers or not

def check (value): 

fo r letter in value: 

 

# If anything other than hexdigit

# email is present, then return

# False, otherwise return True

if letter not in string.hexdigits: 

return False

return True

 
Driver code

input1 = "0123456789abcdef"

print (input1, "- & gt; " , check (input1)) 

  

input2 = "abcdefABCDEF"

print (input2, "- & gt;" , check (input2)) 

  

input3 = "abcdefghGEEK"

print (input3, "- & gt;" , check (input3)) 

Exit:

 0123456789abcdef - & gt; True abcdefABCDEF - & gt; True abcdefghGEEK - & gt; False 

Applications:
The string constant hexdigits can used in many practical applications ... Let`s see some code explaining how to use numbers to generate strong random passwords of a given size.

# Import random ones to generate
# random sequence of lines

import random 

 
# Import string library function

import string 

  

def rand_pass (size): 

 

# Takes a random selection from

  # strin g.hexdigits

generate_pass = `` .join ([random.choice (string.hexdigits) 

for n in range (size)]) 

 

  return generate_pass 

  
Driver code

password = rand_pass ( 10

print (password) 

Output:

 e497FEe2bC