In Python, you can generate a password with uppercase, lowercase letters and numbers using the string
and random
module. Here's an example code snippet:
main.py569 chars20 lines
In this code, we first import the required modules random
and string
. The string
module has constants ascii_uppercase
, ascii_lowercase
, and digits
which contain all uppercase and lowercase letters, and numbers respectively.
We define a function generate_password
that takes a length as an input parameter. It creates a pool of characters by concatenating all uppercase, lowercase letters and digits.
Using random.choice
function, it randomly chooses a character from this pool of characters length
number of times, and concatenates all the chosen characters to form a password.
Finally, the function returns the generated password.
Example usage:
main.py70 chars3 lines
gistlibby LogSnag