Random String Generator

  • Share this:

Code introduction


This function generates a random string of specified length containing uppercase and lowercase letters and digits.


Technology Stack : string, random

Code Type : String generation

Code Difficulty : Intermediate


                
                    
import string
import random

def generate_random_string(length):
    if not isinstance(length, int) or length < 0:
        raise ValueError("Length must be a non-negative integer")

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