Generate Random Strings and Passwords in Python

In Python, multiple modules and functions are used to perform different tasks. For instance, the math module provides functions that are used to perform basic to advanced mathematical calculations, Pillow mobile is used to manipulate images, etc.

To create a random string or password the “string” and “random” modules are used in Python. 

This blog post demonstrates various examples to generate random strings using upper and lower-case letters, symbols, digits, etc. 

How to Generate Random Strings and Passwords in Python?

To generate the random strings and passwords the “random” and “string” modules are used in Python. The random strings may contain letters, symbols, and digits. 

Let’s practice different examples to create the random strings and passwords in Python:

Example 1: Using Upper and Lower Case Letters

The below code generates the random strings using the upper and lower case letters:

Code:

import random
import string
string_length = 5
letters = string.ascii_letters
output = ''.join(random.choice(letters) for i in range(string_length))
print(output)

In the above code:

  • The “random” and “string” modules are imported at the start of the program.
  • The random string length is assigned as an integer value to a variable named “string_length”.
  • The “string.ascii_letters()” function is used to get the upper and lower case letters.
  • The “random.choice()” function returns the random letters of length “5”.
  • The “join()” function is used to join the random string value to an empty string.

Output:

The random string containing the upper and lowercase letters has been generated in the above output.

Example 2: Using Lower Case Letters

The below code generates the random strings using the lower case letters:

Code:

import random
import string
string_length = 5
letters = string.ascii_lowercase
output = ''.join(random.choice(letters) for i in range(string_length))
print(output)

In the above code:

  • The “string.ascii_lowercase()” function is used to get the lowercase letters only. 
  • The “random.choice()” function returns the random letters of length “5”.
  • The newly generated random strings and passwords contain lowercase letters only.

Output:

The random string containing the lowercase letters has been generated successfully.

Example 3: Using Upper Case Letters

In the below code the “string” and “random” modules are used to generate the random string using the upper case letters:

Code:

import random
import string
string_length = 5
letters = string.ascii_uppercase
output = ''.join(random.choice(letters) for i in range(string_length))
print(output)

In the above code:

  • The “string.ascii_uppercase()” function is used to represent the uppercase letters only.
  • The “random.choice()” takes the string length and creates the random strings in upper case letters.

Output:

The random string containing the upper case letter has been generated in the above output.

Example 4: Using Specific Strings

The below code generates the random string by utilizing the specific strings:

Code:

import random
string_length = 5
letters = 'abcdxyzpqr'
output = ''.join(random.choice(letters) for i in range(string_length))
print(output)

In the above code:

  • The user-defined specific characters are initialized in the program in string format.
  • The “join()” function is used along with the “for loop” to create a random string from the given string.

Output:

The above snippet verifies the creation of random strings from the given string.

Example 5: Using Letters, Digits, and Symbols

The below code uses the letters, digits, and symbols to generate the random strings:

Code:

import random
import string
string_length = 5
letters = string.ascii_letters + string.digits + string.punctuation
output = ''.join(random.choice(letters) for i in range(string_length))
print(output)

In the above code:

  • The “string.ascii_letters()”, “string.digits()”, and “string.punctuation()” functions are used to get a string of upper and lower case letters, digits and symbols.
  • The “random.choice()” function accepts the letters as an argument and generates the random string of a specific length.

Output:

The passwords or random string has been created successfully.

Conclusion

To generate the random strings and passwords the random and string modules are used in Python. The random string consists of upper and lower case letters, digits, symbols, etc. The random strings can also be created from the specific strings using the “random.choice()” function. Random strings can also be created with letters, symbols, and digits altogether. This Python guide provided an in-depth overview of how to generate random strings and passwords in Python.