Python | Generate random string of given length
The issue of generation of random numbers is quite common, but sometimes, we have applications that require us to better that and provide some functionality of generating a random string of digits and alphabets for applications such as passwords. Let’s discuss certain ways in which this can be performed in Python. Here, we will use Random string generation with upper case letters and digits
Method 1: Generate a random string using random.choices()
This random.choices() function of a random module can help us achieve this task, and provides a one-liner alternative to a whole loop that might be required for this particular task. Works with Python > v3.6.
- String.ascii_uppercase – It returns the string with uppercase.
- String.digits – It returns the string with digits.
- String.punctuation – It returns the string with punctuations
- String.ascii_letters – It returns the string containing various cases.
- String.ascii_lowercase – It returns the string with lowercase.
Example 1: Random string generation with upper case letters
Python3
import string import random # initializing size of string N = 7 # using random.choices() # generating random strings res = ''.join(random.choices(string.ascii_uppercase + string.digits, k = N)) # print result print ( "The generated random string : " + str (res)) |
Output :
The generated random string : 0D5YE91
Example 2: Generate a random string of a given length in Lowercase
Python3
import string import random # initializing size of string N = 7 # using random.choices() # generating random strings res = ''.join(random.choices(string.ascii_lowercase + string.digits, k = N)) # print result print ( "The generated random string : " + str (res)) |
Output:
The generated random string : ipxktny
Example 3: Generate a random string of a given length in Uppercase and Lowercase
Python3
import string import random # initializing size of string N = 7 # using random.choices() # generating random strings res = ''.join(random.choices(string.ascii_letters, k = N)) # print result print ( "The generated random string : " + str (res)) |
Output:
The generated random string : ALpxvmI
Method 2: Generate random string using secrets.choice()
We can Generate Random Strings and Passwords in Python using secrets.choice(). For Cryptographically more secure random numbers, this function of the secret module can be used as its internal algorithm is framed in a way to generate less predictable random numbers.
Python3
import secrets import string # initializing size of string N = 7 # using secrets.choice() # generating random strings res = ''.join(secrets.choice(string.ascii_uppercase + string.digits) for i in range (N)) # print result print ( "The generated random string : " + str (res)) |
The generated random string : T7HPKVR
Approach:
Import the necessary modules: random and string.
Define a function named generate_random_string that takes in a parameter length to determine the length of the random string to generate.
Use string.ascii_letters to get all the ASCII letters in lowercase and uppercase.
Use random.choice to randomly choose characters from letters for the given length of the string, and concatenate them using ”.join().
Return the generated string.
Use the function by passing in the desired length to generate a random string.
Python3
import random import string def generate_random_string(length): # Get all the ASCII letters in lowercase and uppercase letters = string.ascii_letters # Randomly choose characters from letters for the given length of the string random_string = ''.join(random.choice(letters) for i in range (length)) return random_string # Example usage: generate a random string of length 10 random_string = generate_random_string( 10 ) print (random_string) |
mFPltLThhj
Time complexity: O(n), where n is the length of the desired random string.
Auxiliary Space: O(n), where n is the length of the desired random string.
Please Login to comment...