In this tutorial, I will explain how to generate a random string of a specific length in Python.
I encountered this issue while working on a project for generating unique coupon codes for a retail store in the United States. Each coupon needed to be a random string of a specific length to ensure uniqueness and prevent duplication. Let us explore how to generate a random string of a specific length in Python.
Random String Generation
Before getting into the code, let’s understand what we mean by a random string. A random string is a sequence of characters chosen randomly from a predefined set of characters. The set can include letters (uppercase and lowercase), digits, punctuation marks, or any combination of these.
Let us see some methods to achieve this task.
Read How to Convert a String to ASCII in Python?
1. Use the random Module
Python’s built-in random module provides functions for generating random numbers and selecting random elements from sequences. We can leverage this module to generate random strings.
Read How to Swap Characters in a String Using Python?
Generate a Random String with Letters and Digits
Let’s say we want to generate a random string of length 12 consisting of uppercase letters and digits. Here’s how we can achieve this using the random module:
import random
import string
def generate_random_string(length):
characters = string.ascii_uppercase + string.digits
random_string = ''.join(random.choice(characters) for _ in range(length))
return random_string
# Example usage
random_string = generate_random_string(12)
print(random_string) Output:
6W1CFABB5UZLI have executed the above example code and added the screenshot below.

In this example, we define a function generate_random_string that takes the desired length as a parameter. We create a string characters that contains all the uppercase letters (using string.ascii_uppercase) and digits (using string.digits).
Then, we use list comprehension to generate a random character for each position in the string. The random.choice function selects a random character from the characters string. We use the join method to concatenate the random characters into a single string.
Read How to Check if a Given String is an Anagram of Another String?
Generate a Random String with a Specific Character Set
In some cases, you may want to generate a random string using a specific set of characters. For example, let’s generate a random string of length 10 using only lowercase letters:
import random
import string
def generate_random_string(length):
characters = string.ascii_lowercase
random_string = ''.join(random.choice(characters) for _ in range(length))
return random_string
# Example usage
random_string = generate_random_string(10)
print(random_string) Output:
zkqzvgssknI have executed the above example code and added the screenshot below.

Here, we modify the characters string to include only lowercase letters using string.ascii_lowercase. The rest of the code remains the same.
Read How to Check if a String is an Emoji in Python?
2. Use the secrets Module
Python’s secrets module provides secure random number generation for cryptographic purposes. While it’s primarily designed for security-sensitive applications, it can also be used to generate random strings.
Generate a Secure Random String
Let’s generate a secure random string of length 16 using a combination of letters, digits, and punctuation marks:
import secrets
import string
def generate_secure_random_string(length):
characters = string.ascii_letters + string.digits + string.punctuation
random_string = ''.join(secrets.choice(characters) for _ in range(length))
return random_string
# Example usage
secure_random_string = generate_secure_random_string(16)
print(secure_random_string)Output:
bJFd}UyF9(|AOnkdI have executed the above example code and added the screenshot below.

In this example, we use the secrets.choice function instead of random.choice to select random characters from the characters string. The secrets module provides a more secure way of generating random values compared to the random module.
Read How to Check if a String Contains Any Special Character in Python?
Conclusion
In this tutorial, I have explained how to generate a random string of a specific length in Python. task using the built-in random and secrets modules. By leveraging these modules and specifying the desired length and character set, you can easily create random strings for various purposes, such as generating unique identifiers or temporary passwords.
You may also like to read:
- How to Check if String Length is Greater Than 0 in Python?
- How to Check if a String is Comma Separated in Python?
- How to Check if a String is a GUID in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.