How to Generate a Random String in Python

In this tutorial, we will learn how to generate strings of varying lengths using different combinations of characters in Python.

 

To obtain sets of characters for our random string generator, we will use the Python string package. Let's have a look at character sets available in this package so we know what combinations of characters we can use in our function.

 

  • ascii_lowercase – lower case letter.
  • ascii_uppercase – upper case letters.
  • ascii_letters – both upper and lower case letters.
  • digits – integers from 0-9.
  • punctuation – all special characters: !”#$%&'()*+,-./:;<=>?@[\]^_`{|}~.
  • whitespace – all "space" characters, including tab, linefeed, return and vertical tab.
  • printable – all printable characters including whitespaces.

 

Generate A String with Random Upper and Lower Case Letters

In the function below, we will create a list of all the letters in the alphabet (upper and lower case) using the .ascii_letters property from the string package and randomly select 10 using the .choice() function from the random package.

 

import string
import random

def getRandLetters(number):
   
  return ''.join(random.choice(string.ascii_letters) for i in range(number))

result = getRandLetters(10)

print(result)
GstiZoyhjY

 

Get a String of Specific Letters at Random

To generate a random string containing specific letters, supply a string of those letters to random.choice() like this:

 

import random

def getRandLetters(number, letters):
   
  return ''.join(random.choice(letters) for i in range(number))

result = getRandLetters(10, 'abcxyz')

print(result)
azxzcxbzbz

 

Generate a Random String Without Repeating Characters

The .choice() function will select any character even if it has already been used. If you want to create random strings without repeated character, use the .sample() function instead like this:

 

import string
import random

def getRandLetters(number):
   
  return ''.join(random.sample(string.ascii_letters, number))

result = getRandLetters(10)

print(result)
kKaXoZHEVx

 

Essentially, .sample() picks a random sample from a supplied iterable without using a particular character more than once.

 

Random String from All Possible Characters

To generate a random string from all possible printable characters, use the .printable property from the string package in a function like this:

 

import string
import random

def getRandLetters(number):
   
  return ''.join(random.choice(string.printable) for i in range(number))

result = getRandLetters(10)

print(result)
4\E2#|_4 <

 

From this point, you can play about with different types of characters to include in your random strings by combining character sets from the string package in different ways.

 

import string
import random

def getRandLetters(number):
   
  chars = string.ascii_letters + string.digits + string.punctuation
   
  return ''.join(random.choice(chars) for i in range(number))

result = getRandLetters(16)

print(result)
uc[W[8-}{0w.L3m{

 

A nice addition to the above example would be to add user-defined character set selection.