Random String Generator

  • Share this:

Code introduction


Generates a random string of a specified length using a set of characters including uppercase and lowercase letters.


Technology Stack : random, string

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
import string
import datetime
import os
import sys
import re
import math
import collections

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

    Args:
        length (int): The length of the random string to generate.
        letters (str): The set of characters to use in the random string.

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