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_letters
  • string.ascii_lowercase
  • string.ascii_uppercase
  • string.digits
  • string.hexdigits
  • string.letters
  • string.lowercase
  • string.octdigits
  • string.punctuation
  • string.printable
  • string.uppercase
  • string.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:

  1. The function randStr() takes two parameters: chars (defaulting to uppercase letters and digits) and N (the length of the random string).
  2. The string is generated by repeatedly picking random characters from the chars string and joining them together.
  3. 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
d35bbbfcea

Additional 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:

  1. This program generates a random string consisting of digits only by using string.digits as the character group.
  2. We generate random strings of different lengths: 8 digits by default, and 5 digits in the second case.

Output

52038761
31492

2. 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:

  1. This program generates a random string consisting only of punctuation characters by using string.punctuation as the character set.
  2. 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:

  1. Here, we generate random strings consisting only of uppercase letters by using string.ascii_uppercase.
  2. The length of the random string is customizable as shown in the examples.

Output

FWHEFJGA
XFTMBJ

Summary

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.


Python Libraries