Open In App

Python string | hexdigits

Last Updated : 16 Oct, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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

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

Returns : Return all hexadecimal digit letters.

Note : Make sure to import string library function inorder to use string.hexdigits

Code #1 :




# import string library function 
import string 
    
# Storing the value in variable result 
result = string.hexdigits 
    
# Printing the value 
print(result) 


Output :

0123456789abcdefABCDEF

 

Code #2 : Given code checks if the string input has only hexadecimal digit letters




# importing string library function 
import string 
     
# Function checks if input string 
# has only hexdigits or not 
def check(value): 
    for letter in value: 
             
        # If anything other than hexdigit 
        # letter is present, then return 
        # False, else return True 
        if letter not in string.hexdigits: 
            return False
    return True
     
# Driver Code 
input1 = "0123456789abcdef"
print(input1, "--> ",  check(input1)) 
     
input2 = "abcdefABCDEF"
print(input2, "--> ", check(input2)) 
     
input3 = "abcdefghGEEK"
print(input3, "--> ", check(input3)) 


Output:

0123456789abcdef -->  True
abcdefABCDEF -->  True
abcdefghGEEK -->  False

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




# Importing random to generate 
# random string sequence 
import random 
    
# Importing string library function 
import string 
    
def rand_pass(size): 
        
    # Takes random choices from 
    # string.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


Similar Reads

String slicing in Python to check if a string can become empty by recursive deletion
Given a string “str” and another string “sub_str”. We are allowed to delete “sub_str” from “str” any number of times. It is also given that the “sub_str” appears only once at a time. The task is to find if “str” can become empty by removing “sub_str” again and again. Examples: Input : str = "GEEGEEKSKS", sub_str = "GEEKS" Output : Yes Explanation :
2 min read
String slicing in Python to Rotate a String
Given a string of size n, write functions to perform following operations on string. Left (Or anticlockwise) rotate the given string by d elements (where d <= n).Right (Or clockwise) rotate the given string by d elements (where d <= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeek
3 min read
Python | Check if given string can be formed by concatenating string elements of list
Given a string 'str' and a list of string elements, write a Python program to check whether given string can be formed by concatenating the string elements of list or not. Examples: Input : str = 'python' lst = ['co', 'de', 'py', 'ks', 'on'] Output : False Input : str = 'geeks' lst = ['for', 'ge', 'abc', 'ks', 'e', 'xyz'] Output : True Approach #1
5 min read
Python | Check if string ends with any string in given list
While working with strings, their prefixes and suffix play an important role in making any decision. For data manipulation tasks, we may need to sometimes, check if a string ends with any of the matching strings. Let's discuss certain ways in which this task can be performed. Method #1 : Using filter() + endswith() The combination of the above func
6 min read
Python | Sorting string using order defined by another string
Given two strings (of lowercase letters), a pattern and a string. The task is to sort string according to the order defined by pattern and return the reverse of it. It may be assumed that pattern has all characters of the string and all characters in pattern appear only once. Examples: Input : pat = "asbcklfdmegnot", str = "eksge" Output : str = "g
2 min read
Python | Merge Tuple String List values to String
Sometimes, while working with records, we can have a problem in which any element of record can be of type string but mistakenly processed as list of characters. This can be a problem while working with a lot of data. Let's discuss certain ways in which this problem can be solved. Method #1: Using list comprehension + join() The combination of abov
6 min read
Python | Sort each String in String list
Sometimes, while working with Python, we can have a problem in which we need to perform the sort operation in all the Strings that are present in a list. This problem can occur in general programming and web development. Let's discuss certain ways in which this problem can be solved. Method #1 : Using list comprehension + sorted() + join() This is
4 min read
Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isdigit() This task can be performed using a combinat
6 min read
Python - Length of shortest string in string list
Sometimes, while working with a lot of data, we can have a problem in which we need to extract the minimum length string of all the strings in list. This kind of problem can have applications in many domains. Let’s discuss certain ways in which this task can be performed. Method #1 : Using min() + generator expression The combination of the above f
5 min read
Python - Remove front K characters from each string in String List
Sometimes, we come across an issue in which we require to delete the first K characters from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is always a plus. Let’s discuss certain ways in which this can
6 min read
Article Tags :
Practice Tags :