create a password with a custom length and uppercase letters in python

You can create a password with a custom length and uppercase letters by using the random module and generating a random string of characters of the desired length.

Here's an example code snippet:

main.py
import random
import string

def generate_password(length):
    # defining the character set
    characters = string.ascii_letters + string.digits + string.punctuation
    
    # generating random password
    password = ''.join(random.choice(characters) for i in range(length))
    
    # converting some characters to uppercase
    password = password[:length//2] + password[length//2:].upper()
    
    return password

# example usage
password_length = 12
password = generate_password(password_length)
print(password)
522 chars
20 lines

In this example, generate_password is a function that takes a length argument for the password to be generated. The characters variable is a string that contains all the possible characters that can appear in the password. The random.choice function is used to select a random character from the characters string for each character in the password.

To ensure that some characters in the password are uppercase, we split the string into two halves and convert the second half to uppercase using the str.upper() method.

Finally, the function returns the generated password. You can adjust the password_length variable to change the length of the generated password.

gistlibby LogSnag