How to Generate Random Strings with Upper Case letters and Digits in Python?

The generation of random strings has a variety of applications, particularly in the realm of cryptographic security. Python gives several options for this.

Here, let us see how to generate a random string that only contains uppercase letters and numerals.

You should be aware that Python has two major libraries for generating random strings:

  • The String module contains a number of string constants, including those for uppercase, lowercase letters, digits, symbols, and so on.
  • The Random module is used to generate pseudo-random numbers.
  • The Secrets module generates cryptographically strong random numbers appropriate for passwords, authentication methods, and so on.

The random module has the below methods to generate the random string:

  • string.ascii_letters: It generates a random string with both uppercase and lowercase characters.
  • string_ascii_uppercase: It generates a random string that only returns uppercase letters.
  • string.ascii_lowercase: It generates a random string that only returns lowercase letters.
  • string.digits: It generates a random string that returns a string containing numerical characters.
  • string.punctuation: It generates a random string with punctuation characters

NOTE:

  • The random.choice() function may generate random strings with repeating characters.
  • You can use random.sample() if you want non-repeating characters.
  • If you want a more cryptographically secure string, you must use the new secrets.choice() method in Python 3.6.

Generating Random Strings with Upper Case letters and Digits in Python

Example1: Using random.choice method

Approach:

  • Import string module using the import keyword
  • Import random module using the import keyword
  • Create a function say genenate_randomstr() that accepts the string length as an argument
  • Inside the function, take an empty list and store it in a variable.
  • Loop till the given length of a string using the for loop
  • Generate a random string with uppercase characters and digits(numbers) using the string.ascii_uppercase, and string.digits functions by passing it as an argument to the random.choice() function.
  • Append the obtained random string to the above declared new string using the append function
  • Convert the above new list into string using the join() function and return it.
  • Pass some random string length as an argument to the above genenate_randomstr() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import string module using the import keyword
import string
# Import random module using the import keyword
import random
# Create a function say genenate_randomstr() that accepts the string length as an argument
def genenate_randomstr(str_len):
    # Inside the function, take an empty list and store it in a variable.
    new_lst=[]
    # Loop till the given length of a string using the for loop
    for itr in range(str_len):
        # Generate a random string with uppercase characters and digits(numbers) using the 
        # string.ascii_uppercase, and string.digits functions by passing it as an argument 
        # to the random.choice() function.
        # Append the obtained random string to the above declared new string using the append function
        new_lst.append(random.choice(string.ascii_uppercase + string.digits))
    # Convert the above new list into string using the join() function and return it.
    return(''.join(new_lst))

# Pass some random string length as an argument to the above genenate_randomstr() function
# and print the result
print("The random string with uppercase characters and digits is:")
print(genenate_randomstr(12))

Output:

The random string with uppercase characters and digits is:
B1F7HSYJYX0W

Example2: Code in a single line

# Import string module using the import keyword
import string
# Import random module using the import keyword
import random
# Generating a random string with uppercase characters and digits using random.choice(),
# string.ascii_uppercase, and string.digits functions
''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(12))

Output:

J9M9B5DNDSK2

Generating a random string using random.choices() method

The random.choices() function in Python3 allows a second argument that specifies the length of the string. This can be used to generate even shorter code. By simply defining the length of the string with the second parameter k, you can avoid the looping procedure.

# Import string module using the import keyword
import string
# Import random module using the import keyword
import random
# Generating a random string with uppercase characters and digits using random.choices(),
# string.ascii_uppercase, and string.digits functions
''.join(random.choices(string.ascii_uppercase + string.digits, k=12))

Output:

XF3HWUSU82PR