Random String Generator

  • Share this:

Code introduction


This function is used to generate a random string of a specified length. The string can contain both uppercase and lowercase letters and digits.


Technology Stack : random, string

Code Type : Generate random strings

Code Difficulty : Intermediate


                
                    
def random_string(length, letters='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):
    import random
    import string
    return ''.join(random.choice(string.ascii_letters + string.digits) for i in range(length))                
              
Tags: