Wednesday, 26 August 2026

๐Ÿš€ Day 104/150 – OTP Generator in Python

 



๐Ÿš€ Day 104/150 – OTP Generator in Python

One-Time Passwords (OTPs) are widely used to verify user identity during login, registration, online payments, and password recovery. Python provides multiple ways to generate OTPs, ranging from simple random numbers to cryptographically secure methods suitable for real-world applications.

In this post, we'll explore four different ways to generate OTPs in Python.


Method 1 – Random 4-Digit OTP

The simplest way to generate an OTP is by creating a random 4-digit number.

import random otp = random.randint(1000, 9999) print("Your OTP is:", otp)




Sample Output

Your OTP is: 4831

Explanation

import random imports Python's random module.

random.randint(1000, 9999) generates a random integer between 1000 and 9999.

The generated number is printed as the OTP.

This method is easy to understand and suitable for learning purposes.

Note: The random module is not recommended for security-sensitive applications.

Method 2 – Random 6-Digit OTP

Many websites and mobile applications use 6-digit OTPs because they provide more possible combinations.

import random otp = random.randint(100000, 999999) print("Your OTP is:", otp)




Sample Output

Your OTP is: 824175

Explanation

random.randint(100000, 999999) generates a random 6-digit number.

Since the minimum value is 100000, leading zeros are avoided.

This method is commonly used in practice for basic OTP generation.


Method 3 – OTP Using Digits

Instead of generating a random integer, we can build an OTP by randomly selecting digits.

import random import string otp = "".join(random.choices(string.digits, k=6)) print("Your OTP is:", otp)




Sample Output

Your OTP is: 593804

Explanation

string.digits contains all numeric characters (0123456789).

random.choices() randomly selects 6 digits.

"".join() combines those digits into a single string.

Since the OTP is a string, it can start with 0, which is useful in many authentication systems.


Method 4 – Secure OTP Generator

For real-world applications, Python's secrets module provides a more secure way to generate OTPs.

import secrets import string otp = "".join(secrets.choice(string.digits) for _ in range(6)) print("Your Secure OTP is:", otp)




Sample Output

Your Secure OTP is: 071638

Explanation

secrets is designed for generating cryptographically secure random values.

secrets.choice() selects one random digit securely.

The loop runs 6 times to generate a 6-digit OTP.

This method is recommended for authentication systems, banking applications, and password reset features.

Tip: Whenever security matters, prefer the secrets module over random.


Comparison of Methods

MethodBest For
Random 4-Digit OTP            Basic Python practice
Random 6-Digit OTPSimple OTP generation
OTP Using DigitsFlexible OTP generation with string output
Secure OTP GeneratorReal-world authentication systems

๐Ÿ”ฅ Key Takeaways

  • OTPs are used to verify users during login, registration, and password recovery.
  • random.randint() is the easiest way to generate numeric OTPs.
  • random.choices() allows you to generate OTPs as strings, including leading zeros.
  • The string.digits constant provides all numeric characters for OTP creation.
  • The secrets module is the safest choice for generating secure OTPs in production applications.
  • For security-critical systems, always use secrets instead of random.

Stay tuned for Day 105 of the #150DaysOfPython series! ๐Ÿš€


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (339) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (343) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (89) Coursera (302) Cybersecurity (36) data (10) Data Analysis (46) Data Analytics (31) data management (16) Data Science (421) Data Strucures (18) Deep Learning (216) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (13) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (390) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (34) Python (1362) Python Coding Challenge (1225) Python Library (1) Python Mathematics (13) Python Mistakes (51) Python Quiz (611) Python Tips (102) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (20) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)