Home » Python Program to generate a Random String

Python Program to generate a Random String

by Online Tutorials Library

Python Program to generate a Random String

A random refers to the collection of data or information that can be available in any order. The random module in python is used to generate random strings. The random string is consisting of numbers, characters and punctuation series that can contain any pattern. The random module contains two methods random.choice() and secrets.choice(), to generate a secure string. Let’s understand how to generate a random string using the random.choice() and secrets.choice() method in python.

Python Program to generate a Random String

Using random.choice()

The random.choice() function is used in the python string to generate the sequence of characters and digits that can repeat the string in any order.

Create a program to generate a random string using the random.choices() function.

Random_str.py

Output:

Python Program to generate a Random String

Following are the method used in the random module to generate the random string.

Methods Description
String.ascii_letters It returns a random string that contains both uppercase and lowercase characters.
String_ascii_uppercase It is a random string method that only returns a string in uppercase characters.
String.ascii_lowercase It is a random string method that returns a string only in lowercase characters.
String.digits It is a random string method that returns a string with numeric characters.
String.punctuation It is a random string method that returns a string with punctuation characters.

Generate a random string of upper case and lower-case letters

UprLwr.py

Output:

Python Program to generate a Random String

Random String of Specified Characters

Specific.py

Output:

Python Program to generate a Random String

Note: The random.choice() method is used in the python program to repeat the same characters strings. If we don’t want to display repetitive characters, we should use random.sample() function.

Generate a random string without repeating the same characters

WithoutRepeat.py

Output:

Python Program to generate a Random String

As we can see in the above output, the random.sample() method returns a string in which all characters are unique and non-repeating. Whereas, the random.choice() method returns a string that may contain repetitive characters. So, we can say that if we want to generate a unique random string, use random.sample() method.

Generate a random alphanumeric string consisting of fixed letters and digits

For example, suppose we want a randomly generated alphanumeric string that contains five letters and four digits. We need to define these parameters into the function.

Let’s write a program to generate an alphanumeric string that contains a fixed number of letters and digits.

fixedString.py

Output:

Python Program to generate a Random String

Using secrets.choice()

A secrets.choice() method is used to generate a more secure random string than random.choice(). It is a cryptographically random string generator that ensures no two processes can obtain the same results simultaneously using secrets.choice() method.

Let’s write a program to print a secure random string using the secrets.choice method.

Secret_str.py

Output:

Python Program to generate a Random String

Use the different method of the random module to generate a safe random string.

Let’s write a program to print secure random strings using different methods of secrets.choice().

Secret.py

Output:

Python Program to generate a Random String


You may also like