Random String Generator

  • Share this:

Code introduction


This function generates a random string of a specified length, which can be used for password generation, temporary identifiers, and so on.


Technology Stack : random, string

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
import string
import math
import re
import datetime
import time
import os
import sys
import json
import hashlib
import base64
import html
import email.utils
import urllib.parse
import urllib.request
import urllib.error
import http.client

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.
    """
    letters = string.ascii_letters
    return ''.join(random.choice(letters) for i in range(length))                
              
Tags: