One-time Passwords (OTP) is a password that is valid for only one login session or transaction in a computer or a digital device. Now a days OTP’s are used in almost every service like Internet Banking, online transactions etc. They are generally combination of 4 or 6 numeric digits or a 6-digit alphanumeric.
random() function can be used to generate random OTP which is predefined in random library. Let’s see how to generate OTP using Python.
random.random(): This function returns any random number between 0 to 1.
math.floor(): It returns floor of any floating number to a integer value.
Using the above function pick random index of string array which contains all the possible candidates of a particular digit of the OTP.
Example #1 : Generate 4 digit Numeric OTP
OTP of 4 digits: 3211
Example #2: Generate alphanumeric OTP of length 6
OTP of length 6: pyelJl
Example #3: Using String constants
- Python program to check the validity of a Password
- Password validation in Python
- getpass() and getuser() in Python (Password without echo)
- Python | Random Password Generator using Tkinter
- Generate all permutation of a set in Python
- Generate a graph using Dictionary in Python
- Program to generate all possible valid IP addresses from given string
- Python | Generate QR Code using pyqrcode module
- How to generate byte code file in python ?
- Python | Generate successive element difference list
- Python | Generate random numbers within a given range and store in a list
- Secrets | Python module to Generate secure random numbers
- Generate two output strings depending upon occurrence of character in input string in Python
- Python Program for Efficient program to print all prime factors of a given number
- Python Program for Program to Print Matrix in Z form
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.