Random String Generator

  • Share this:

Code introduction


This function generates a random string of a specified length, composed of uppercase and lowercase letters and numbers.


Technology Stack : random

Code Type : Generate random strings

Code Difficulty :


                
                    
import random

def generate_random_string(length=10):
    """生成指定长度的随机字符串
    
    Args:
        length (int): 字符串长度,默认为10
    
    Returns:
        str: 生成的随机字符串
    """
    characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    return ''.join(random.choice(characters) for i in range(length))                
              
Tags: