Random String Generator

  • Share this:

Code introduction


This function generates a random string of a specified length, which can be used for password generation, captcha, and other scenarios.


Technology Stack : random, string

Code Type : Generate random strings

Code Difficulty : Intermediate


                
                    
import random
import os
import json
import datetime
import hashlib
import re
import math
import html
import sys

def generate_random_string(length=10):
    """
    Generate a random string of a given length.
    
    Args:
        length (int): Length of the random string to generate.
    
    Returns:
        str: A random string of the specified length.
    """
    characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    return ''.join(random.choice(characters) for _ in range(length))                
              
Tags: