Generate a random string with a specific length.

In this python basic program, we will generate a random string with a specific length.

Steps to solve the program
  1. Import string and random library.
  2. Take the length of the string as input through the user.
  3. To generate random string with a specified length use random.choices(string.ascii_letters, k=n), where n is the length.
  4. 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
				
			

generate random numbers.

get the current date.

Leave a Comment