Generate a random string of length N in Python

avatar

Borislav Hadzhiev

Last updated: Sep 21, 2022

banner

Photo from Unsplash

Generate a random string of length N in Python #

To generate a random string of length N:

  1. Use the string module to get a string of the ASCII characters.
  2. Use the random.choice() method to get N random characters from the string.
  3. Use the str.join() method to join the iterable into a string.
main.py
import string import random # ✅ Generate a random string of length N of Lowercase letters lowercase_only = ''.join( random.choice(string.ascii_lowercase) for _ in range(5) ) print(lowercase_only) # 👉️ koahf # ----------------------------------------- # ✅ Generate a random string of length N of Uppercase letters uppercase_only = ''.join( random.choice(string.ascii_uppercase) for _ in range(5) ) print(uppercase_only) # 👉️ TNYAD # ----------------------------------------- # ✅ Generate a random string of length N of Lowercase and Uppercase letters lowercase_and_uppercase = ''.join( random.choice(string.ascii_letters) for _ in range(5) ) print(lowercase_and_uppercase) # 👉️ xJyNJ

The first example generates a random string of length N containing ASCII lowercase letters.

main.py
import string import random lowercase_only = ''.join( random.choice(string.ascii_lowercase) for _ in range(5) ) print(lowercase_only) # 👉️ axfkd

We used the string module to get a string containing the ASCII letters.

main.py
import string # abcdefghijklmnopqrstuvwxyz print(string.ascii_lowercase) # ABCDEFGHIJKLMNOPQRSTUVWXYZ print(string.ascii_uppercase) # abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ print(string.ascii_letters)

The ascii_lowercase attribute returns a string containing the lowercase ASCII letters.

You can use the ascii_uppercase attribute if you need to get a string containing the uppercase ASCII letters or the ascii_letters attribute to get a string containing both.

The random.choice method takes a sequence and returns a random element from the non-empty sequence.

main.py
import string import random print(random.choice(string.ascii_lowercase)) # 👉️ y print(random.choice(string.ascii_lowercase)) # 👉️ j print(random.choice(string.ascii_lowercase)) # 👉️ z

The last step is to use the str.join() method to join the iterable into a string.

main.py
import string import random uppercase_only = ''.join( random.choice(string.ascii_uppercase) for _ in range(5) ) print(uppercase_only) # 👉️ TNYAD

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

We used an empty string to join the elements of the iterable into a string without a delimiter.

To get a random string of length N that contains both lowercase and uppercase ASCII letters, use the string.ascii_letters attribute.

main.py
import string import random lowercase_and_uppercase = ''.join( random.choice(string.ascii_letters) for _ in range(5) ) print(lowercase_and_uppercase) # 👉️ xJyNJ

You can use the string.digits attribute if you need to get a random string of length N containing digits.

main.py
import string import random digits = ''.join( random.choice(string.digits) for _ in range(5) ) print(digits) # 👉️ 71438

The string.digits attribute returns a string containing the digits from 0 to 9.

main.py
import string import random print(string.digits) # 👉️ 0123456789

We used the range() class to construct an iterable in the examples.

The range() class takes the following arguments:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

If you only pass a single argument to the range() constructor, it is considered to be the value for the stop parameter.

main.py
for n in range(5): print(n) # 👉️ 0 1 2 3 4 result = list(range(5)) # 👇️ [0, 1, 2, 3, 4] print(result)
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.