Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Spandan Sehgal
Spandan Sehgal

Posted on • Edited on

     

Python Script to make a Password Generator

Intro

Passwords are most important thing in our online security and the passwords must be strong so that our info can't get leaked.

So in this post, which is my first one I am going to tell you how to make apassword generator in python .

Required Modules

For this python project we will require only two built-in python modules that are :-

  • string
  • random

Use of the modules

  1. string : We will use this module to add different characters to our password so it becomes strong and difficult to crack.

  2. random : We will be using this module to pick random characters .

Let's Code

Start by importing the modules

import randomimport string
Enter fullscreen modeExit fullscreen mode

Now we are going to create our variables which will use string module to store all characters

str1 = string.ascii_lowercase #This will store all the lower case alphabetsstr2 = string.ascii_uppercase This will store all the upper case alphabetsstr3 = string.digits This will store all the digitsstr4 = string.punctuation This will store all the punctuation marks which will make our password more strong
Enter fullscreen modeExit fullscreen mode

Now we will ask user for how much length the password should be and make an empty list which will join our variables in which all characters are stored

pwdlen = int(input("Enter password length\n"))s = []s.extend(list(str1))s.extend(list(str2))s.extend(list(str3))s.extend(list(str4))
Enter fullscreen modeExit fullscreen mode

Now finally we will generate the password and print it

print("Your password is: ")print("".join(random.sample(s, pwdlen)))a = input("")
Enter fullscreen modeExit fullscreen mode

You can access this code at myGitHub Repo
I hope you all liked this post if you have any issue feel free to ask in the comments section

You can also visit mywebsite for more content

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
wjplatformer profile image
Wj
e
  • Email
  • Location
    Singapore
  • Education
    YouTube Videos
  • Work
    Founder of TEL-Code on GitHub
  • Joined

It is a good practice to put variable names that store passwords to be.... weird. Such as "BlueSkyeOnName" For password. This makes it harder for hackers to access stuff, especially if you are building a network with limited cybersecurity knowledge.

CollapseExpand
 
sehgalspandan profile image
Spandan Sehgal
Hey there! I'm a 16 yo curious student who's into coding since 4yrs and writing blogs. Always curious to learn more and discuss!

Ok thanks for the information I'll take care of it next time. 🙂

CollapseExpand
 
morphzg profile image
MorphZG
Web developer and casual writer. Javascript, Node.js, React.js, Python, SQL, Linux... | From full stack web apps to cloud computing.
  • Location
    Croatia
  • Joined

Thanks for sharing. I am also learning python and find this exercise online. I did it a bit different but i like your approach also. You could let the user choose strenght also. Weak password with only letters, medium with added digits and strong with all printable characters.

CollapseExpand
 
sehgalspandan profile image
Spandan Sehgal
Hey there! I'm a 16 yo curious student who's into coding since 4yrs and writing blogs. Always curious to learn more and discuss!

Thanks for your reply stay tuned for more content .
I am glad to hear that you liked it .

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hey there! I'm a 16 yo curious student who's into coding since 4yrs and writing blogs. Always curious to learn more and discuss!
  • Location
    India
  • Education
    Gurugram
  • Pronouns
    he/him
  • Work
    Student
  • Joined

More fromSpandan Sehgal

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp