In this python basic program, we will generate a random string with a specific length.
Steps to solve the program
- Import string and random library.
- Take the length of the string as input through the user.
- To generate random string with a specified length use random.choices(string.ascii_letters, k=n), where n is the length.
- Print the output.
import string
import random
n = int(input("Enter length of the string: "))
result = ''.join(random.choices(string.ascii_letters, k=n))
# print result
print("The generated random string : ",str(result))
Output :
Enter length of the string: 8
The generated random string : HWyGuDWg