create a password with uppercase letters in python

To create a password with uppercase letters in Python, we can use the string module and its ascii_uppercase constant, which contains all uppercase letters in the ASCII character set. Here's an example function that generates a random password using uppercase letters:

main.py
import random
import string

def generate_password(length):
    """Generate a random password with uppercase letters."""
    password = ''
    for i in range(length):
        password += random.choice(string.ascii_uppercase)
    return password
245 chars
10 lines

This function takes a parameter length which determines the length of the password to generate. Inside the function, we initialize an empty string password and then loop length number of times. In each iteration of the loop, we randomly choose a character from string.ascii_uppercase using random.choice() and append it to the password string.

To call the function and generate a password of length 8, for example, we would write:

main.py
password = generate_password(8)
print(password)
48 chars
3 lines

This would output a randomly generated password consisting of 8 uppercase letters, such as 'XJGKPQFH'.

gistlibby LogSnag