Linux Hint Posted June 30, 2023 Share Posted June 30, 2023 One of the common tasks that Python programmers perform is generating random strings for various purposes, such as passwords, tokens, IDs, etc. Python’s inbuilt “random” module provides various functions for generating random strings containing upper and lowercase letters/characters along with digits. This blog will present a complete tutorial on generating random strings in Python. How to Generate Random String in Python? The following methods are utilized to generate random Python strings: “choices()” Method. “choice()” Method. Method 1: Python Random String Generation Using the “random.choices()” Method In Python, the “random.choices()” method takes the sequence such as list, string, etc., and retrieves multiple random elements with a replacement value. Syntax random.choices(sequence, weights=None, cum_weights=None, k=1) In the above syntax: The “sequence” parameter specifies sequences such as lists, strings, tuples, etc. The “weights”, and “cum_weights” are optional parameters that indicate the weight possibility of each value. The “k” parameter indicates the string length. Example 1: Generating Random String With Uppercase and Digits In this example code, the “random.choices()” method is utilized to retrieve a random string: import string import random output = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5)) print("Generated Random String : " + str(output)) In the above code, the “random.choices()” method takes the “string.ascii_uppercase”, and “string.digits” methods, and the string length size as its parameters, respectively and retrieves a random string. This random string is appended to an empty string using the “join()” method. Output The random string has been generated in uppercase successfully. Example 2: Generating Random String With Both Lower and Upper Cases The below code generates random strings with both the lower and upper cases: import string import random output = ''.join(random.choices(string.ascii_letters, k=5)) print("Generated Random String : " + str(output)) In the above code lines, the “random.choices()” method takes the “string.ascii_letters” method and string length as its parameters and retrieves a random string containing both the upper and lower case letters. Output The random string comprising both the character’s cases has been created. Method 2: Python Random String Generation Using the “secrets.choice()” Method In Python, the “secrets.choice()” method takes the non-empty sequence as an argument and retrieves random elements/items. This method can also be employed in Python to generate a random string. Syntax secrets.choice(sequence) Example The below code snippet is utilized to generate a random string: import secrets import string output = ''.join(secrets.choice(string.ascii_lowercase + string.digits) for i in range(5)) print("Generated Random String : " + str(output)) In the above code, the “secrets.choice()” method takes the “string.ascii_lowercase” and “string.digits” constant as its arguments and retrieves a random string of the specified length. The returned random string length is appended to an empty string via the “join()” method. Output The random string has been generated successfully. Conclusion The “choices()” method of the “random” module or the “choice()” method of the “secrets” module is used to retrieve a random string in Python. The methods such as the “string.ascii_uppercase”, “string.digits”, “string.ascii_letters”, and “string.ascii_lowercase” are passed inside the specified functions to randomly return the string with a unique value. This write-up presented various ways of Python string generation using numerous examples. View the full article Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.