In this post, I will show you how to generate a random string of length n in Python.
for example, If You want to generate a random string of fixed length 10.
Generate a fixed length random string In Python :-
To generate a random string in Python we need to use the following two Python modules.
String module :- This module contains the various string constant which contains the ASCII characters of all cases.
String module contains separate constants for lowercase letters, uppercase letters, digits, and special characters.
random module :- random module perform the operations of random String generations.
Python Program to generate a fixed length random string In Python :-
Let’s Suppose we want to generate a random string of length 10 then we need to execute a random.choice() function 10 times to pick a single letter from the string.ascii_lowercase
and add it to the string variable.
import random import string def random_String(string_Length): """Generating a random string of fixed length in Python """ letter = string.ascii_lowercase return ''.join(random.choice(letter) for j in range(string_Length)) print ("Random String is ", random_String(5) ) print ("Random String is ", random_String(10) ) print ("Random String is ", random_String(15) ) print ("Random String is ", random_String(20) )
Output:-
Random String is qhpgx Random String is azervvbyoj Random String is gtfagyjrpjsfrdm Random String is ktesldwisbjbjikwlhcv
Note: In the above program, output contains all the upper case letters. If you want only the lowercase letters, then use the string.ascii_lowercase instead of a string.ascii_uppercase.
Python program Generate a random string contains lower case and upper case letters:-
import random import string def random_String(string_Length): """Generating a random string containslowercase and uppercase letters """ letter = string.ascii_letters return ''.join(random.choice(letter) for j in range(string_Length)) print("Random String contains of lowercase and uppercase letters") print ("First Random String is ", random_String(7) ) print ("second Random String is ", random_String(10) ) print ("second Random String is ", random_String(15) ) print ("second Random String is ", random_String(20) )
Output:-
Random String contains of lowercase and uppercase letters First Random String is ZYupyBO second Random String is CQeEZtROqb second Random String is ZAbyHndlqxTHsai second Random String is oXhFFabxtcQRRGNOmsct
Note: The random.choice method can repeat the characters in generated string. If you don’t want repeated characters in generated String, then kindly use the random.sample function.
Python program to generate random string contains letters and digits in Python:-
Many times we require a string which contains both letters and digit for random password generation.
For example, random string :- Ab23cFd, jkLml18, 127texki.
To generate random string contains letters and digits in Python we need to use the string.ascii_letters and string.digits constants two get the combinations of letters and digits in our random string.
import random import string def random_String(string_Length=6): """Generating a random string containing letters and digits """ lettersAndDigits = string.ascii_letters + string.digits return ''.join(random.choice(lettersAndDigits) for j in range(string_Length)) print ("Generating a Random String with letters and digits") print ("First Random String is ", random_String(8)) print ("Second Random String is ", random_String(10)) print ("Third Random String is ", random_String(18)) print ("Third Random String is ", random_String())
output:-
Generating a Random String with letters and digits First Random String is JHYva6I7 Second Random String is jy5BfcIQ4Y Third Random String is fK8luYIuqEQzszQSgm Third Random String is FBoB2t
Python Program To generate a random password string with Special characters, letters, and digits in Python:-
If password contains characters, digits and special symbols are considered as a strong password.
For example, if we want to generate a random Password like below:-
ab2@5efd#$
j*&k%m&32
T871t@h*ki%
Python Program to Generate a random password string with Special characters, letters, and digits
In this Python program we are going to use string.ascii_letters , string.digits and string.punctuation along with random.choice function.
import random import string def random_String(string_Length=10): """Generating a random string which contains letters, digits and special characters """ password_characters = string.ascii_letters + string.digits + string.punctuation return ''.join(random.choice(password_characters) for j in range(string_Length)) print("Generating Random String password containg letters, digits and special characters ") print ("First Random String ", random_String() ) print ("Second Random String", random_String(13) ) print ("Third Random String", random_String(15) ) print ("fourth Random String", random_String(20) )
Output:-
Generating Random String password containg letters, digits and special characters First Random String QE$z7k&uM\ Second Random String mXKFN>U,j-p!) Third Random String g7^!Ttc0B"'8@tG fourth Random String ':e'2xXy!QeZw.oC,m&e
Python Program to generate a random password String which contains lowercase character,uppercase character, digits and special character in Python.
import random import string def random_Password(): """Generating a random password contains uppercase,lowercase,digit and symbol """ random_Source = string.ascii_letters + string.digits + string.punctuation Random_password = random.choice(string.ascii_lowercase) Random_password += random.choice(string.ascii_uppercase) Random_password += random.choice(string.digits) Random_password += random.choice(string.punctuation) for j in range(6): Random_password += random.choice(random_Source) password_List = list(Random_password) random.SystemRandom().shuffle(password_List) password = ''.join(password_List) return password print ("First Random Password is ", random_Password()) print ("Second Random Password is ", random_Password()) print ("Third Random Password is ", random_Password()) print ("Fourth Random Password is ", random_Password())
First Random Password is dtA3+'S`gA Second Random Password is -C:5DlF?4$ Third Random Password is 34Vc{=%@MX Fourth Random Password is W+r96y$OD"