Python: Generate Random Strings with Uppercase Letters and Digits
You can generate random strings with combinations of uppercase letters and digits in Python by using various functions such as random.choices()
, uuid4()
,
and secrets.choice()
functions.
The following example explains how to generate random strings with uppercase letters and digits.
Using random.choices()
function
You can use random.choices()
function to generate random strings with upper case letters and digits of specified length.
For example, if you want to create random strings of length 10 with upper case letters and digits.
# import packages
import random
import string
# create upper case string and Digits
all_chars = string.ascii_uppercase + string.digits
str_len = 10
# generate random strings with upper case letters and digits
rand_string = ''.join(random.choices(all_chars, k=str_len))
print(rand_string)
# output
YTJ8DO13R3
In the above example, the all_chars
variable contains all uppercase letters and digits, and then we use this variable to generate random strings
of length 10.
You can see that the random strings of length 10 with uppercase letters and digits have been created using the random.choices()
function.
Using secrets.choice()
function
In addition to random.choices()
, you can use secrets.choice()
function to generate random strings with uppercase letters and digits of specified length.
For example, if you want to create random strings of length 10 with upper case letters and digits.
# import packages
import secrets
import string
# create upper case string and Digits
all_chars = string.ascii_uppercase + string.digits
str_len = 10
# generate random strings with upper case letters and digits
rand_string = ''.join(secrets.choice(all_chars) for i in range(str_len))
print(rand_string)
# output
71B4ZXAKS8
You can see that the random strings of length 10 with uppercase letters and digits have been created using the secrets.choice()
function.
The secrets.choice()
is more useful for generating random strings for sensitive data like passwords or authentication tokens.
Using uuid4()
function
You can also use the uuid4()
function fromt the built-in uuid package to generate random strings of specified length with uppercase letters and digits.
For example, if you want to create random strings of length 10 with uppercase letters and digits.
# import packages
import uuid
# create random string
all_chars = str(uuid.uuid4()).upper().replace("-", "")
str_len = 10
# generate random strings with upper case letters and digits
rand_string = ''.join(random.choices(all_chars, k=str_len))
print(rand_string)
# output
6876A1A44A
In the above example, the uuid4()
generates a unique identifier containing hyphens, digits, and lowercase characters.
The lowercase characters are converted to uppercase using the upper()
function and hyphens are removed by the replace()
function.
You can see that the random strings of length 10 with uppercase letters and digits have been created using the uuid4()
and random.choices()
function.