Unique ID Generator with Prefix and Length

  • Share this:

Code introduction


Generates a unique ID with a specified prefix and length.


Technology Stack : random, string

Code Type : Function

Code Difficulty : Intermediate


                
                    
def unique_id_generator(prefix="ID", length=8):
    import random
    import string

    def _random_string(length):
        letters = string.ascii_letters + string.digits
        return ''.join(random.choice(letters) for i in range(length))

    return f"{prefix}_{_random_string(length)}"                
              
Tags: