Write a Python program to generate a random string.

How to generate a random string in python. Write a python program to generate a random string.

How to generate a random string?

Here, we’ll see how we can produce arbitrary strings in Python. As the name recommends, we have to create an irregular arrangement of characters, it is appropriate for the arbitrary module.

There are different methodologies here, so we’ll begin from the most natural one; utilizing randomized integers.

What is a random string?

Input: Generate a random string

Output: pslGG

Algorithm to generate a random string:

  •  Import random and string to generate a random string.
  •  Assign a value to N which will be the length of your string.
  •  Use the join() function to join the randomly generated string and assign that to a variable where string.ascii_letters + string. digits give the output by combining randomly generated uppercase, lowercase, and digits.
  • Print the new variable to get the randomly generated string.

Python program to generate a random string.

import string
import random
N = 5
res = ''.join(random.choices(string.ascii_letters + string.digits, k=N))
print("The generated random string : " + str(res))

Output: Printing the randomly generated string.