# Solution to Password Generator using Random and String package in Python

**URL:** <https://forum.codewithmosh.com/t/solution-to-password-generator-using-random-and-string-package-in-python/28610>\
**Category:** Python\
**Created:** [September 8, 2024, 4:39am UTC](https://forum.codewithmosh.com/t/solution-to-password-generator-using-random-and-string-package-in-python/28610 "2024-09-08T04:39:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gourav97](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/gourav97/32/8436_2.png) [@Gourav97](https://forum.codewithmosh.com/u/Gourav97)\
**Post date:** [September 8, 2024, 4:39am UTC](https://forum.codewithmosh.com/t/solution-to-password-generator-using-random-and-string-package-in-python/28610/1 "2024-09-08T04:39:55Z")

</div>

Here is the solution to Password Generator using Random and String package in Python which is given below:

import string  
import random

length = int(input("Enter password length: "))

print(‘’‘Choose character set for password from these :  
1. Digits  
2. Letters  
3. Special characters  
4. Exit’‘’)

characterList = “”  
while (True):  
choice = int(input("Pick a number "))  
if (choice == 1):  
characterList += string.ascii\_letters  
elif (choice == 2):  
characterList += string.digits  
elif (choice == 3):  
characterList += string.punctuation  
elif (choice == 4):  
break  
else:  
print(“Please pick a valid option!”)

password = []

for i in range(length):  
randomchar = random.choice(characterList)  
password.append(randomchar)  
print("The random password is " + “”.join(password))
