Random String Generator

  • Share this:

Code introduction


Generates a random string of a specified length, which can contain uppercase and lowercase letters and numbers.


Technology Stack : random, string

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
import string
import re
import math
import time
import datetime
import os

def generate_random_string(length, characters=string.ascii_letters + string.digits):
    """Generate a random string of a given length.

    Args:
        length (int): Length of the random string to generate.
        characters (str): String of characters to use in the random string.

    Returns:
        str: A random string of the specified length.
    """
    return ''.join(random.choice(characters) for i in range(length))                
              
Tags: