You can download this code by clicking the button below.
This code is now available for download.
This function generates a random password of specified length, including uppercase and lowercase letters, digits, and special characters.
Technology Stack : random, string
Code Type : Generate random password
Code Difficulty : Intermediate
import random
import string
import re
import math
import sys
import os
def generate_random_password(length=8):
if not isinstance(length, int) or length < 1:
raise ValueError("Password length must be a positive integer.")
characters = string.ascii_letters + string.digits + string.punctuation
return ''.join(random.choice(characters) for i in range(length))