How to generate random strings with upper case letters and digits in Python?


You generate random strings with upper case letters and digits in Python. Here are three different ways to do it:

Using random.choices() and string.ascii_uppercase

In this method, we will use the random.choices() function to randomly choose characters from the string.ascii_uppercase and string.digits strings to generate a random string.

Example

We first import the random and string modules, which we will use to generate random characters and create strings, respectively.

We define the length of the random string we want to generate (in this example, it's 10).

We define the possible characters that can be used to create the random string, which is a combination of string.ascii_uppercase (all uppercase letters) and string.digits (all digits).

We use random.choices() function to randomly select k (which is equal to the length of the random string) characters from the characters string.

We then join these characters together into a single string using the .join() method.

Finally, we print the resulting random string.

import random
import string
# define the length of the random string
length = 10
# define the possible characters that can be used
characters = string.ascii_uppercase + string.digits

# use random.choices() to generate the random string
result = ''.join(random.choices(characters, k=length))
# print the result
print(result)

Output

ZMVICXPMKQ

Using random.choice() and a loop

In this method, we will use a for loop and random.choice() function to generate random characters and add them to a string until we have the desired length.

Example

We first import the random and string modules, which we will use to generate random characters and create strings, respectively.

We define the length of the random string we want to generate (in this example, it's 10).

We define the possible characters that can be used to create the random string, which is a combination of string.ascii_uppercase (all uppercase letters) and string.digits (all digits).

We define an empty string variable called result to hold the random characters as they are generated.

We use a for loop to generate a new random character using random.choice() function and add it to the result string for each iteration of the loop, until we have the desired length of the random string.

Finally, we print the resulting random string.

import random
import string
# define the length of the random string
length = 10
# define the possible characters that can be used
characters = string.ascii_uppercase + string.digits

# define an empty string to hold the random characters
result = ''
# use a loop to generate the random string
for i in range(length):
    result += random.choice(characters)
# print the result
print(result)

Output

Y9V1OVO4H4

Using uuid.uuid4() and str.replace()

In this method, we will use the uuid.uuid4() function to generate a random UUID (Universally Unique Identifier) and then use the str.replace() method to remove the hyphens and convert the string to all uppercase letters.

Example

We import the uuid module, which we will use to generate a random UUID.

We use the uuid.uuid4() function to generate a random UUID and convert it to a string using the str() method.

We use the .replace() method to remove the hyphens from the UUID string and replace them with an empty string

import uuid
# generate a random UUID
random_uuid = str(uuid.uuid4())
# remove the hyphens and convert to uppercase letters
result = random_uuid.replace('-', '').upper()
# print the result
print(result)

Output

3D9FF7622A494CABA861D0EE62547BED

Using random.sample() and string.ascii_uppercase

In this method, we will use the random.sample() function to randomly select k characters (where k is the desired length of the random string) from the string.ascii_uppercase and string.digits strings.

Example

We first import the random and string modules, which we will use to generate random characters and create strings, respectively.

We define the length of the random string we want to generate (in this example, it's 10).

We define the possible characters that can be used to create the random string, which is a combination of string.ascii_uppercase (all uppercase letters) and string.digits (all digits).

We use random.sample() function to randomly select k (which is equal to the length of the random string) characters from the characters string.

We then join these characters together into a single string using the .join() method.

Finally, we print the resulting random string.

import random
import string
# define the length of the random string
length = 10
# define the possible characters that can be used
characters = string.ascii_uppercase + string.digits
# use random.sample() to generate the random string
result = ''.join(random.sample(characters, k=length))
# print the result
print(result)

Output

VJMFQO43XL

Using random.randint() and chr()

In this method, we will use the random.randint() function to generate random integers between the ASCII codes for '0' and 'Z', and then use the chr() function to convert these integers into their corresponding ASCII characters.

Example

We import the random module, which we will use to generate random integers and characters.

We define the length of the random string we want to generate (in this example, it's 10).

We use random.randint() function to generate a random integer between the ASCII codes for '0' (which is 48) and 'Z’(which is 90), and then convert this integer into its corresponding ASCII character using the chr() function.

We repeat this process for the desired length of the random string using a for loop, and join these characters together into a single string using the .join() method.

Finally, we print the resulting random string.

import random
# define the length of the random string
length = 10
# use random.randint() and chr() to generate the random string
result = ''.join(chr(random.randint(48, 90)) for _ in range(length))
# print the result
print(result)

Output

W0FW?RL2?Z

These are some examples of how to generate random strings with uppercase letters and digits in Python. You can choose the method that best fits your needs and preferences.

Updated on: 10-Aug-2023

564 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements