We earn commission when you buy through affiliate links.

This does not influence our reviews or recommendations.Learn more.

Learn to code a password generator in Pythonto generate secure passwordsusing the Pythonsecretsmodule.

Random-Password-Generator

In this tutorial, youll learn how to create a secure and random password generator in Python.

Heres why you should consider coding a password generator in Python.

Most of the websites require you to sign on with credentials.

python-password-generator

This is a repetitive task that you’ve got the option to automate using a Python script.

you’re able to use Python to programmatically generatesecurepasswords.

This lets you trigger the script to generate passwords as neededwithout worrying about the password being secure.

2

you might also add a graphical user interface using libraries such as Tkinter, and much more!

The Random Module in Python: Is It Secure Enough?

Pythonrandom modulealso provides functions that let you sample from an alphabet and constructseeminglyrandom sequences.

python-secrets-password-generator

The random module generatespseudo-randomsequences.

Let us parse what pseudo-randomness means.

The term pseudo-random signifies that even though the sequence may appear random, it actually isdeterministic.

alphabet

This is why you should not use random for applications where security is important, such as passwords.

So when can you use therandommodule?

For all applications that are not sensitive, it’s possible for you to use the random module.

You would like to assign a particular set to each student at random.

Here, we want it to contain the following: lowercase and uppercase letters and special characters.

Thestringmodule provides string constants that we can use to define the alphabet.

Well use the following string constants:

Finally, lets concatenate the above string constants to get the alphabet.

In this example, the password string is of length 12.

When the generated password,pwdsatisfies the constraints, we break out of the infinite loop.

Heres the complete code for the Python password generator:

Conclusion

Congratulations on finishing this tutorial!

Youve now learned how to code a password generator in Python.

Next, read thelist of Python web serversto use.