Random Password Generator

  • Share this:

Code introduction


This function is used to generate a random password of a specified length, which consists of uppercase and lowercase letters, digits, and special characters.


Technology Stack : os, re, random, string, datetime

Code Type : Generate random password

Code Difficulty : Intermediate


                
                    
import os
import re
import random
import string
import datetime

def random_password(length=12):
    if not isinstance(length, int) or length < 6:
        raise ValueError("Password length must be an integer greater than or equal to 6.")
    
    characters = string.ascii_letters + string.digits + "!@#$%^&*()_+"
    return ''.join(random.choice(characters) for i in range(length))