Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Categorize Password as Strong or Weak using Regex in Python
Next article icon

Resetting Password Using Python

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this discussion, we explore creating a robust Python password reset tool using the regex library. We define a specific pattern for the password format, enhancing security. The tool incorporates parameters ensuring passwords meet criteria like uppercase/lowercase letters, digits, and special characters. It strategically uses Python libraries, such as getpass for secure input and time for a delay, adding security, and improving user experience. The article provides a step-by-step process, offering insights into each development stage. By the end, you'll not only have a functional password reset tool but also a deep understanding of Python's libraries and regex for secure password management.

Reset Password Using Python

Follow the steps below to reset a password using Python.

Step 1: Install Necessary libraries

In this step, we are going to install getpass and regex library.

pip install getpass4

Step 2: Import Libraries

In this step will be importing all the required libraries in our Python program.

Python3
import re import time from getpass import getpass 

Step 3: Create a Pattern For Your Password Using Regex

In this step we will be creating a pattern for the password to be entered by the user. We be following some basic case for password such as

  • A password must starts with capital letter.
  • A password must contains a digit, a special character.
  • It must be at least 8 characters long.

Breakdown of the pattern

  • (?=.*[a-z]|[A-Z]) this will check if it contains a letter or not.
  • (?=.*\d) this will check for digit.
  • (?=.*[!@#$%^&*()_+{}|:<>?/~`]) this will check for special character.
  • [A-Za-z\d!@#$%^&*()_+{}|:<>?/~`]{8,} this will check for length.
Python3
#create a pattern for your desired paasword type pattern = re.compile(r'^(?=.*[a-z]|[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+{}|:<>?/~`])[A-Za-z\d!@#$%^&*()_+{}|:<>?/~`]{8,}$')  def check(password):     if bool(pattern.match(password)) and password[0].isupper():         return True     return False 

Step 4: Create a Log in Method

In this step we will create a logged-In method that will be log us in our website or app after successfully resetting the password.

Python3
def loggedIn():     #Login to the wesbiste/App      time.sleep(5)     print("Logged in Sucessfully.........") 

Step 5: Create a Main Method

In this step, we'll establish the main method responsible for receiving user input and validating specified conditions. Let's examine the code implementation. Within this step, the check() method is invoked, verifying the basic measures prompted to the user during password entry. Additionally, we prompt the user to input the password twice, allowing for correction of any typing errors unnoticed during the initial entry. This step encompasses the invocation of all the previously created methods.

Python3
def main():     print("Entered password must:-")     print(" -Starts with Capital Letter")     print(" -conatins a digit and special character")     print(" -at least 8 characters long")          password1 = getpass("Enter the Password: ")      if check(password1)==False:         print("Enter a vaild password")     else :         password2 = getpass("Enter the Password again: ")          if password1 == password2:             print("Successfully changed password")             loggedIn()         else :           print("You entered a differnt password")  if __name__ == '__main__':     main() 

Complete Code

Python3
import re import time from getpass import getpass  #create a pattern for your desired paasword type pattern = re.compile(r'^(?=.*[a-z]|[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+{}|:<>?/~`])[A-Za-z\d!@#$%^&*()_+{}|:<>?/~`]{8,}$')  def check(password):     if bool(pattern.match(password)) and password[0].isupper():         return True     return False  def loggedIn():     #Login to the wesbiste/App      time.sleep(5)     print("Logged in Sucessfully")  def main():     print("Entered password must:-")     print(" -Starts with Capital Letter")     print(" -conatins a digit and special character")     print(" -at least 8 characters long\n")          password1 = getpass("Enter the Password: ")      if check(password1)==False:         print("Enter a vaild password")     else :         password2 = getpass("Enter the Password again: ")          if password1 == password2:             print("Successfully changed password")             loggedIn()         else :           print("You entered a differnt password")  if __name__ == '__main__':   main() 

Output :

Gfgresetter
output



Next Article
Categorize Password as Strong or Weak using Regex in Python

V

vishuvaishnav3001
Improve
Article Tags :
  • Python
  • Geeks Premier League
  • OpenAI API
  • Geeks Premier League 2023
Practice Tags :
  • python

Similar Reads

  • Generating Strong Password using Python
    Having a weak password is not good for a system that demands high confidentiality and security of user credentials. It turns out that people find it difficult to make up a strong password that is strong enough to prevent unauthorized users from memorizing it. This article uses a mixture of numbers,
    3 min read
  • Storing passwords with Python keyring
    In this article, we will see how to store and retrieve passwords securely using Python's keyring package. What is a keyring package? keyring package is a module specifically designed to securely store and retrieve passwords. It is like the keychain of MacOS, using this module and Python code we can
    2 min read
  • Python | Random Password Generator using Tkinter
    With growing technology, everything has relied on data, and securing this data is the main concern. Passwords are meant to keep the data safe that we upload on the Internet. An easy password can be hacked easily and all personal information can be misused. In order to prevent such things and keep th
    4 min read
  • Create a Random Password Generator using Python
    In this article, we will see how to create a random password generator using Python.  Passwords are a means by which a user proves that they are authorized to use a device. It is important that passwords must be long and complex. It should contain at least more than ten characters with a combination
    5 min read
  • Categorize Password as Strong or Weak using Regex in Python
    Given a password, we have to categorize it as a strong or weak one. There are some checks that need to be met to be a strong password. For a weak password, we need to return the reason for it to be weak. Conditions to be fulfilled are: Minimum 9 characters and maximum 20 characters.Cannot be a newli
    2 min read
  • Setting and Updating User Passwords in PostgreSQL
    Setting and updating user passwords in PostgreSQL is good for securing our database from unauthorized access. By managing user credentials and configuring authentication methods we can enhance control over user roles and ensure data security. In this article, we will discuss how to set and update pa
    4 min read
  • Python String replace() Method
    The replace() method replaces all occurrences of a specified substring in a string and returns a new string without modifying the original string. Let’s look at a simple example of replace() method. [GFGTABS] Python s = "Hello World! Hello Python!" # Replace "Hello" with "Hi
    2 min read
  • Python string | ascii_uppercase
    In Python3, ascii_uppercase is a pre-initialized string used as a string constant. In Python, the string ascii_uppercase will give the uppercase letters ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’. Syntax : string.ascii_uppercase Parameters: Doesn't take any parameter, since it's not a function. Returns: Return al
    2 min read
  • How to check a valid regex string using Python?
    A Regex (Regular Expression) is a sequence of characters used for defining a pattern. This pattern could be used for searching, replacing and other operations. Regex is extensively utilized in applications that require input validation, Password validation, Pattern Recognition, search and replace ut
    6 min read
  • Telnet Automation / Scripting Using Python
    Telnet is the short form of Teletype network, which is a client/server application that works based on the telnet protocol. Telnet service is associated with the well-known port number - 23. As Python supports socket programming, we can implement telnet services as well. In this article, we will lea
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences