Random String Generator

  • Share this:

Code introduction


This function generates a random string of specified length, used for password generation, captcha generation, etc.


Technology Stack : String manipulation, random number generation

Code Type : Function

Code Difficulty : Intermediate


                
                    
import re
import json
import os
import random
import string
import sys

def generate_random_string(length=10):
    """生成一个指定长度的随机字符串。

    Args:
        length (int): 字符串长度,默认为10。

    Returns:
        str: 生成的随机字符串。
    """
    letters = string.ascii_letters + string.digits
    result_str = ''.join(random.choice(letters) for _ in range(length))
    return result_str