Python Program to Generate Random String With Uppercase And Digits
Generating a series of random strings can help create security codes. Besides, there are many other applications for using a random string generator, for instance, obtaining a series of numbers for a lottery game or slot machines. A random string generator generates an alphanumeric string consisting of random characters and digits. Let’s see how we can create a random string generator.
Method 1: Using for loop
For generating random strings only with uppercase letters and digits using for loop, we require to import built-in python modules, namely random and string.
# Random string generation using uppercase letters and digits
# YTDWIU75
import random
import string
def id_generator(length):
# initializing empty string
return_str = ""
# generating a string containing A-Z and 0-9
data = string.ascii_uppercase + '0123456789'
for i in range(length):
# generating random strings
return_str += random.choice(data)
# print result
print("The generated random string : " + str(return_str))
# function call
id_generator(7)
Output
The generated random string : 0ZM1C29
Method 2: Using secrets.choice()
The secrets module generates cryptographically secure random numbers suitable for managing data such as passwords, account authentication, and security tokens.
import secrets
import string
# initializing empty string
return_str = ""
# initializing size of string
N = 7
# generating random strings using secrets.choice()
return_str = ''.join(secrets.choice(string.ascii_uppercase + string.digits)
for i in range(N))
# print result
print("The generated random string : " + str(return_str))
Output
The generated random string : 26SJ8IA
Method 3: Using random.choices()
The third approach is by random.choices. The choices() function returns a collection of random elements with replacement.
import random
import string
# initializing empty string
return_str = ""
# initializing size of string
N = 7
# generating random strings using random.choices()
return_str = ''.join(random.choices(
string.ascii_uppercase + string.digits, k=N))
# print result
print("The generated random string : " + str(return_str))
Output
The generated random string : H4ICGAZ
Method 4: Using secrets module
step-by-step algorithm for implementing this approach:
- Import the secrets and string modules.
- Define the id_generator function with a single argument, length.
- Initialize an empty string, return_str.
- Generate a string containing uppercase letters and digits (0-9) using string.ascii_uppercase and the string literal ‘0123456789’.
- Use a list comprehension to generate a list of length random characters from the data string using secrets.choice(data).
- Use the join method to concatenate the characters in the list into a single string.
- Assign the resulting string to return_str.
- Print the generated random string with a message.
- Return the generated string.
import secrets
import string
def id_generator(length):
# initializing empty string
return_str = ""
# generating a string containing A-Z and 0-9
data = string.ascii_uppercase + '0123456789'
# using the secrets module to generate the random string
return_str = ''.join(secrets.choice(data) for i in range(length))
# print result
print("The generated random string : " + str(return_str))
# function call
id_generator(7)
Output
The generated random string : YB7HXVH
The time complexity of this algorithm is O(n), where n is the length of the string to be generated. The secrets.choice() method is called n times to generate the random characters, and the join() method is called once to concatenate the characters into a string.
The auxiliary space of this algorithm is also O(n), where n is the length of the string to be generated. This is because the function creates a list of length n to hold the random characters, and then concatenates them into a string of length n. The space used by the data string and the return_str variable is constant, regardless of the length of the generated string.