Random String Generator

  • Share this:

Code introduction


This function generates a random string of a specified length, which is used in scenarios such as password generation and unique identifiers.


Technology Stack : os, sys, time, datetime, random, string

Code Type : Generate random strings

Code Difficulty : Intermediate


                
                    
import os
import sys
import time
import datetime
import random
import json
import re
import string

def generate_random_string(length=10):
    """Generate a random string of a given length.

    Args:
        length (int): The length of the random string to generate.

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