DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

How to Generate a random string of a given length in Python?

ItsMyCode |

In Python, Generating a random string is pretty straightforward. However, there are scenarios where we need to generate a random string that includes alphanumeric characters for a strong password.

There are various approaches to achieve this functionality.

Generate a random Mixed case alphabet string

If you want to generate a mixed case alphabet random string, then you string.ascii_letters and random.choice() for a given specified length as shown below.

import random
import string

def generateRandomString(length):
    # Generate mixed case alphabets string
    letters = string.ascii_letters
    result_str = ''.join(random.choice(letters) for i in range(length))
    print("Random string of length", length, "is:", result_str)

generateRandomString(8)

# Output
# Random string of length 8 is: hNIgRMoC
Enter fullscreen mode Exit fullscreen mode

Generate a random Upper case alphabet string

If you want to generate a random upper case alphabet string, then you string.ascii_uppercase and random.choice() for a given specified length as shown below.

import random
import string

def generateRandomUpperCaseString(length):
    # Generate upper case alphabets string
    letters = string.ascii_uppercase
    result_str = ''.join(random.choice(letters) for i in range(length))
    print("Random upper case string of length", length, "is:", result_str)

generateRandomUpperCaseString(10)

# Output
# Random upper case string of length 10 is: FXFUJHAUOJ      
Enter fullscreen mode Exit fullscreen mode

Generate a random Lower case alphabet string

If you want to generate a random lower case alphabet string, then you string.ascii_lowercase and random.choice() for a given specified length as shown below.

import random
import string

def generateRandomLowerCaseString(length):
    # Generate lower case alphabets string
    letters = string.ascii_lowercase
    result_str = ''.join(random.choice(letters) for i in range(length))
    print("Random lower case string of length", length, "is:", result_str)

generateRandomLowerCaseString(10)

# Output
# Random lower case string of length 10 is: rtssovidqa         

Enter fullscreen mode Exit fullscreen mode


python

Generate a random Alphanumeric string

If you want to generate a random alphanumeric string, then you string.ascii_lowercase + string.digits and random.choice() for a given specified length as shown below.

import random
import string

def generateRandomAlphaNumericString(length):
    # Generate alphanumeric string
    letters = string.ascii_lowercase + string.digits
    result_str = ''.join(random.choice(letters) for i in range(length))
    print("Random alphanumeric string of length", length, "is:", result_str)

generateRandomAlphaNumericString(12)

# Output
# Random alphanumeric string of length 12 is: ae8nd6149q7j         
Enter fullscreen mode Exit fullscreen mode

Generate a random strong password using string module

If you want to generate a strong password using the string module, then you string.ascii_letters + string.digits + string.punctuation and random.choice() for a given specified length as shown below.

import random
import string

def generateRandomStrongPassword(length):
    # Generate random strong password
    letters = string.ascii_letters + string.digits + string.punctuation
    result_str = ''.join(random.choice(letters) for i in range(length))
    print("Strong Password of length", length, "is:", result_str)

generateRandomStrongPassword(12)

# Output
# Strong Password of length 12 is: Au}h]D=aJ~QN 
Enter fullscreen mode Exit fullscreen mode

Noterandom.choice()can repeat the characters. If you don’t want to repeat the characters, then use random.sample() method.

Generate a random strong password using the secrets module

If you are looking for a secure and robust password, Python has a module called as secrets , and you could utilize this to generate a random secured password. The algorithm used by secrets is less predictable when compared to the random string module generation.

import secrets
import string

def generateRandomCryptoPassword(length):
    # Generate random password using cryptographic way
    letters = string.ascii_lowercase + string.digits + string.punctuation
    result_str = ''.join(secrets.choice(letters) for i in range(length))
    print("Strong Password using Crpyto of length", length, "is:", result_str)

generateRandomCryptoPassword(12)

# Output
# Strong Password using Crpyto of length 12 is: ^6cg1(d^o/~(
Enter fullscreen mode Exit fullscreen mode

Note: This module works perfectly with Python 3.6 and above.

The post How to Generate a random string of a given length in Python? appeared first on ItsMyCode.

Top comments (0)