You can download this code by clicking the button below.
This code is now available for download.
Generate a random string of a specified length, which can be used for password generation, random naming, etc.
Technology Stack : random, string
Code Type : Function
Code Difficulty : Intermediate
import random
import string
import re
import json
import math
import datetime
import os
import sys
def generate_random_string(length=10):
"""生成指定长度的随机字符串
Args:
length (int, optional): 字符串长度. Defaults to 10.
Returns:
str: 生成的随机字符串
"""
letters = string.ascii_letters + string.digits
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str