Random String Generator with Prefix

  • Share this:

Code introduction


This function is used to generate a random string that can include letters and digits. The user can specify the length of the string and the prefix.


Technology Stack : Python, random, string

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
def generate_random_string(length, prefix=""):
    import random
    import string

    characters = string.ascii_letters + string.digits
    return prefix + ''.join(random.choice(characters) for _ in range(length))