Generate random strings only with uppercase letters and digits in Python

The generation of random strings has various use cases, especially in the field of cryptographic security. Python provides various options for the same. In this tutorial, you will learn how to generate a random string that consists of uppercase letters and digits only.

You must be aware that Python provides two main modules for random string generation:

  1. The String module contains various string constants with specific ones for uppercase, lowercase letters, digits, symbols etc.
  2. The Random module for generating pseudo-random numbers.
  3.  Also, the Secrets module for generating cryptographically strong random numbers, suitable for passwords, authentication measures etc.

You can use the above two modules for creating random strings of any pattern and size.
In this tutorial, you will learn how to generate a random string that consists of uppercase letters and digits only.

By looping through a list of characters and joining it to get a string:

import string
import random
def gen_random_string(leng):
    random_list=[]
    for i in range(leng):
        random_list.append(random.choice(string.ascii_uppercase + string.digits))
    return(''.join(random_list))
print(gen_random_string(9))
ALPGQY3QQ

In the above program;

  1. We first import the necessary modules for the random string generation.
  2. We then define a function to generate the random string that consists of only uppercase letters and numbers. Also, the length of the random string to be generated is specified as an argument to the function.
  3. We then initialize an empty list which we shall use for storing the random letters and digits.
  4. The random module’s choice() is a method that returns a single character from the string.
    The string.ascii_uppercase returns a string with uppercase characters while the string.digits returns a string with numeric characters. A concatenation of the two gives us a string of uppercase letters and numbers.
  5. Further, the random module’s choice() returns a random character from the string for each loop cycle.
    The list that by the end of the loop contains random numbers and uppercase letters is then joined to an empty string to get a random string of the specified length.

Observe:
You can run the same code multiple times and observe that you end up getting different strings each time, thanks to the random.choice() method.

Note:

  • The random.choice() generates random strings in which characters may repeat. If you want non-repeating characters, you can use random.sample().
  • If you want a cryptographically more secure string, you must make use of python 3.6’s new secrets.choice() method.

Shortcut method to get the same output with a single line of code:

import random
import string
''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(9))
'U8TG095R5'

So far you have seen the random.choice() method. Python 3’s random.choices() has a second argument that accepts the length of the string. This can be used to get an even shorter piece of code. You can skip the looping process by simply specifying the length of the string with the second argument k.

import string
import random
''.join(random.choices(string.ascii_uppercase + string.digits, k=9))
'XQ5MUEFJK'

random() module in Python 
Random Number String Generation in Python

Leave a Reply

Your email address will not be published. Required fields are marked *