Random Password Generator

  • Share this:

Code introduction


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


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

Code Type : Generate random password

Code Difficulty : Beginner


                
                    
import datetime
import os
import re
import random
import string

def random_password(length=8):
    if length < 8:
        raise ValueError("Password length must be at least 8 characters.")
    return ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=length))