In simple words, it is a sequence of hexadecimal digits, i.e. it is a combination of the digits 0-9 and the characters A-F and a-f. In Python3, string.hexdigits is a pre-initialized string used as string constant. In Python, string.hexdigits will give the hexadecimal letters ‘0123456789abcdefABCDEF’.
Syntax: string.hexdigits
Syntax : string.hexdigits
Parameters : Doesn’t take any parameter, since it’s not a function.
Returns : Return all hexadecimal digit letters.
Also note that this is not a function and therefore no parameters will be fetched.
- Importing a random module and module string to use their respective operations.
- Then we ask the user for the length of the password he needs.
- Use string.hexdigits to generate a hexadecimal string.
- The random.choice () function to select any character from the string generated by the string.hexdigits operation,
- We use join () to join the elements of an array and convert them to a string using the map function
Example #1
def api_config(): """Request API config from user and set""" code, hash_value = DIALOG.inputbox("Enter your API Hash") if code == DIALOG.OK: if len(hash_value) != 32 or any(it not in string.hexdigits for it in hash_value): DIALOG.msgbox("Invalid hash") return string1 = "HASH = "" + hash_value + """ code, id_value = DIALOG.inputbox("Enter your API ID") if not id_value or any(it not in string.digits for it in id_value): DIALOG.msgbox("Invalid ID") return string2 = "ID = "" + id_value + """ with open(os.path.join(utils.get_base_dir(), "api_token.py"), "w") as file: file.write(string1 + " " + string2 + " ") DIALOG.msgbox("API Token and ID set.")
Example #2
def isHex(val: str) -> bool: """ Return whether the given str represents a hex value or not :param val: the string to check :return: whether the given str represents a hex value """ if isinstance(val, bytes): # only decodes utf-8 string try: val = val.decode() except ValueError: return False return isinstance(val, str) and all(c in string.hexdigits for c in val) # decorator
Example #3
def s_hexlit(s, t): if s.peek(2, True) != ’0x’: return False s.get() s.get() lit = "" while not s.empty() and s.peek() in string.hexdigits: lit += s.get() if not lit: return False t.append(int(lit, 16)) return True
Example #4
def s_hexlit(s, t): if s.peek(2, True) != ’0x’: return False s.get() s.get() lit = "" while not s.empty() and s.peek() in string.hexdigits: lit += s.get() if not lit: return False t.append((TokenTypes.LITERAL_INT, int(lit, 16))) return True
Example #5
def post(self, digest): """ Create a running instance from the container image digest """ # Containers are mapped to teams user_account = api.user.get_user() tid = user_account[’tid’] # fail fast on invalid requests if any(char not in string.hexdigits + "sha:" for char in digest): raise PicoException("Invalid image digest", 400) # Create the container result = api.docker.create(tid, digest) return jsonify({"success": result[’success’], "message": result[’message’]})
Archived version
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:
|
Output:
0123456789abcdefABCDEF
Code # 2: This code checks if the string input contains only hexadecimal digits
|
Exit:
0123456789abcdef -" True abcdefABCDEF -" True abcdefghGEEK -" 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.
|
Output: stro ng>
e497FEe2bC