You can download this code by clicking the button below.
This code is now available for download.
This function generates a random password of a specified length, consisting of letters, digits, and special characters.
Technology Stack : random, string
Code Type : Generate random password
Code Difficulty : Intermediate
import random
import string
import os
import math
import datetime
import re
def generate_random_password(length=12):
"""
Generate a random password with a given length.
"""
if length < 8:
raise ValueError("Password length should be at least 8 characters.")
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
return password