Random Password Generator

  • Share this:

Code introduction


This function generates a random password of specified length, including uppercase and lowercase letters, digits, and special characters.


Technology Stack : random, string, re

Code Type : Generate random password

Code Difficulty : Intermediate


                
                    
import random
import string
import re

def generate_random_password(length=8):
    if not isinstance(length, int) or length < 1:
        raise ValueError("Length must be a positive integer")

    characters = string.ascii_letters + string.digits + string.punctuation
    password = ''.join(random.choice(characters) for i in range(length))
    return password                
              
Tags: