• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Using Python to Generate Random String of Specific Length

Using Python to Generate Random String of Specific Length

May 8, 2022 Leave a Comment

To generate a random string of specified length in Python, you can use the random module, comprehension and the Python join() function.

import string
from random import randint

def random_char():
    alphabet = list(string.ascii_lowercase)
    return alphabet[randint(0,25)]

def random_string(n):
    return ''.join(random_char() for _ in range(n))

print(random_string(12))
print(random_string(12))
print(random_string(12))

#Output:
avfsqkkbydlx
xtpbkwjkvqzg
ymmmgdvjqmmr

The ability to randomly generate strings of random characters can be useful in different Python programs.

You can easily generate a random string of specific length in Python with the help of the random module.

First, you need to define the list of characters you want to select from. The Python string module has some great pre-defined constants which can be used to generate lists of characters.

For example, we can get a list of the lowercase letters of the alphabet with the pre-defined string constant ascii_lowercase.

There are also pre-defined constants for the uppercase characters, all characters lowercase and uppercase, and the digits 0 to 9 – to name a few.

After defining your list, you can then create a function which takes a number as an input. This number is the specified length.

With comprehension, we can loop for the specified number and get random characterrs from a helper function.

Finally, we can use the Python join() function to join all the characters together.

Below is a function which generates a random string given a length using Python.

import string
from random import randint

def random_char():
    alphabet = list(string.ascii_lowercase)
    return alphabet[randint(0,25)]

def random_string(n):
    return ''.join(random_char() for _ in range(n))

print(random_string(12))
print(random_string(12))
print(random_string(12))

#Output:
avfsqkkbydlx
xtpbkwjkvqzg
ymmmgdvjqmmr

If you want to use different character sets to generate the random character, you can adjust the selection list and use the choice() function.

import string
from random import choice

def random_char():
    chars = list(string.ascii_uppercase) + list(string.digits)
    return choice(chars)

def random_string(n):
    return ''.join(random_char() for _ in range(n))

print(random_string(12))
print(random_string(12))
print(random_string(12))

#Output:
5VSE2QISXTUC
WJJCNCEPJTU1
TDQPC62HXL78

Hopefully this article has been useful for you to learn how to use Python to generate a random string of specific length.

Other Articles You'll Also Like:

  • 1.  Using Python to Print Plus or Minus Sign Symbol
  • 2.  Python cube root – Find Cube Root of Number With math.pow() Function
  • 3.  Python Get First Word in String
  • 4.  Python Split Tuple into Multiple Variables
  • 5.  Get pandas Series First Element in Python
  • 6.  PROC FREQ Equivalent in Python
  • 7.  Perfect Numbers in Python
  • 8.  Using Python to Check if String Contains Only Letters
  • 9.  How to Iterate through a Set Using Python
  • 10.  Combine Sets in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy