Generate Random String of Specific Length - Python Examples
Generate Random String with Python
A random string may be required for system-generated strong passwords or other scenarios.
Using the random module in Python, you can generate such strings of a specified length and specified character groups.
Step by Step Process
To generate a random string of specific length, follow these steps:
1. Choose Character Group
Choose the character groups from which you would like to pick the characters. The string class provides the following character groups:
string.ascii_lettersstring.ascii_lowercasestring.ascii_uppercasestring.digitsstring.hexdigitsstring.lettersstring.lowercasestring.octdigitsstring.punctuationstring.printablestring.uppercasestring.whitespace
2. Call random.choice()
Use the random.choice() function with all the character groups (appended by the + operator) passed as an argument.
random.choice(string.ascii_uppercase + string.digits)The random.choice() function picks one of the characters from the given string or characters.
3. Repeat Picking the Characters
Repeat this for N times, where N is the length of the random string to be generated.
random.choice(string.ascii_uppercase + string.digits) for _ in range(N)4. Join the Picked Characters
Join all these N characters into a string.
''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))Example
Let us implement all the aforementioned steps to generate a random string of a specific length.
Python Program
import random
import string
def randStr(chars = string.ascii_uppercase + string.digits, N=10):
return ''.join(random.choice(chars) for _ in range(N))
# default length (10) random string
print(randStr())
# random string of length 7
print(randStr(N=7))
# random string with characters picked from ascii_lowercase
print(randStr(chars=string.ascii_lowercase))
# random string with characters picked from 'abcdef123456'
print(randStr(chars='abcdef123456'))Explanation:
- The function
randStr()takes two parameters:chars(defaulting to uppercase letters and digits) andN(the length of the random string). - The string is generated by repeatedly picking random characters from the
charsstring and joining them together. - We demonstrate the function by generating a random string of default length 10, a string of length 7, a string with lowercase letters, and a string from a custom character set
'abcdef123456'.
Output
4FH2R5SQ9D
8STJX1L
iihvalhdwc
d35bbbfceaAdditional Examples
1. Generate Random String from Digits Only
In this example, we will generate a random string using only digits.
Python Program
import random
import string
def randStr(chars = string.digits, N=8):
return ''.join(random.choice(chars) for _ in range(N))
# Random string with digits only
print(randStr())
# Random string of length 5
print(randStr(N=5))Explanation:
- This program generates a random string consisting of digits only by using
string.digitsas the character group. - We generate random strings of different lengths: 8 digits by default, and 5 digits in the second case.
Output
52038761
314922. Generate Random String from Punctuation Characters
In this example, we generate a random string using only punctuation characters.
Python Program
import random
import string
def randStr(chars = string.punctuation, N=6):
return ''.join(random.choice(chars) for _ in range(N))
# Random string with punctuation only
print(randStr())
# Random string of length 4
print(randStr(N=4))Explanation:
- This program generates a random string consisting only of punctuation characters by using
string.punctuationas the character set. - The function is called to generate random strings of specified lengths.
Output
$@%#
?!@3. Generate Random String with Uppercase Letters Only
In this example, we generate a random string using only uppercase letters.
Python Program
import random
import string
def randStr(chars = string.ascii_uppercase, N=8):
return ''.join(random.choice(chars) for _ in range(N))
# Random string with uppercase letters only
print(randStr())
# Random string of length 6
print(randStr(N=6))Explanation:
- Here, we generate random strings consisting only of uppercase letters by using
string.ascii_uppercase. - The length of the random string is customizable as shown in the examples.
Output
FWHEFJGA
XFTMBJSummary
In this tutorial of Python Examples, we learned how to create a random string of a specific length from a given set of characters. We covered the basic implementation, as well as examples for generating random strings with digits, punctuation characters, and uppercase letters.