Password Generator from Username using Python
![](https://static.wixstatic.com/media/bfaec5_069b1de04f5b476dac7a42928b826854~mv2.jpg/v1/fill/w_146,h_82,al_c,q_80,usm_0.66_1.00_0.01,blur_2,enc_auto/bfaec5_069b1de04f5b476dac7a42928b826854~mv2.jpg)
In this post, you will learn how to create a random string passwords in Python. Using a random and string module, we can write our own password generator.
Steps to Create a Random String
1- Import String and Random modules
2- Use the string constant digits and symbols and the function upper() and lower()
3- Use a for loop and random.choice() function to choose characters from a source
4- Generate a password generator
Let's code a function that generates a password from the username.
1- Import modules
![](https://static.wixstatic.com/media/bfaec5_5e4265cfd79d4e059eb620b82cab35aa~mv2.png/v1/fill/w_65,h_6,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/bfaec5_5e4265cfd79d4e059eb620b82cab35aa~mv2.png)
2- Input the username to generate the password
![](https://static.wixstatic.com/media/bfaec5_5b691ecec69841398b799237d98c7171~mv2.png/v1/fill/w_58,h_5,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/bfaec5_5b691ecec69841398b799237d98c7171~mv2.png)
3- Define our function generate_password
![](https://static.wixstatic.com/media/bfaec5_9cf98d75ed2b49378cd08d2bca02f519~mv2.png/v1/fill/w_81,h_7,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/bfaec5_9cf98d75ed2b49378cd08d2bca02f519~mv2.png)
3-1 Convert the username to uppercase and lowercase letters.
store numbers and symbols
"digits contain '0123456789'
punctuation contain all special symbols '!”#$%&'()*+,-./:;<=> @[\]^_`{|}~.' "
![](https://static.wixstatic.com/media/bfaec5_cd4d97957f0943ecbc1eb6dbcd09b941~mv2.png/v1/fill/w_123,h_60,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/bfaec5_cd4d97957f0943ecbc1eb6dbcd09b941~mv2.png)
3-2 Generate random characters from lowercase, uppercase username and all (numbers and symbols)
![](https://static.wixstatic.com/media/bfaec5_b77a4aa6c0f84a3a9deaf3e1c97670ac~mv2.png/v1/fill/w_73,h_15,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/bfaec5_b77a4aa6c0f84a3a9deaf3e1c97670ac~mv2.png)
3-3 Concatenate and return password from username, numbers and symbols
![](https://static.wixstatic.com/media/bfaec5_d3eec48b46734a1eaf99e175d5da74f5~mv2.png/v1/fill/w_67,h_6,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/bfaec5_d3eec48b46734a1eaf99e175d5da74f5~mv2.png)
There is our function below
![](https://static.wixstatic.com/media/bfaec5_00af7bc5a58a4a52aa6799915415c5e0~mv2.png/v1/fill/w_49,h_23,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/bfaec5_00af7bc5a58a4a52aa6799915415c5e0~mv2.png)
Let's test our funtion
![](https://static.wixstatic.com/media/bfaec5_3605fdaab4dd4e528d8bd80e3b9762da~mv2.png/v1/fill/w_75,h_7,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/bfaec5_3605fdaab4dd4e528d8bd80e3b9762da~mv2.png)
Output:
![](https://static.wixstatic.com/media/bfaec5_c4cebe1f0e474dd8ab2a21d04acfb152~mv2.png/v1/fill/w_118,h_14,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/bfaec5_c4cebe1f0e474dd8ab2a21d04acfb152~mv2.png)
You can find the whole project on my Github