Open In App

Python Program to Generate Random String With Uppercase And Digits

Last Updated : 05 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Generating a series of random strings can help create security codes. Besides, there are many other applications for using a random string generator, for instance, obtaining a series of numbers for a lottery game or slot machines. A random string generator generates an alphanumeric string consisting of random characters and digits. Let's see how we can create a random string generator.

Method 1: Using for loop

For generating random strings only with uppercase letters and digits using for loop, we require to import built-in python modules, namely random and string.

Python
# Random string generation using uppercase letters and digits
# YTDWIU75
import random
import string


def id_generator(length):

    # initializing empty string
    return_str = ""

    # generating a string containing A-Z and 0-9
    data = string.ascii_uppercase + '0123456789'

    for i in range(length):

        # generating random strings
        return_str += random.choice(data)

    # print result
    print("The generated random string : " + str(return_str))


# function call
id_generator(7)

Output
The generated random string : 0ZM1C29

 

Method 2: Using secrets.choice()

The secrets module generates cryptographically secure random numbers suitable for managing data such as passwords, account authentication, and security tokens.

Python
import secrets
import string

# initializing empty string
return_str = ""
  
# initializing size of string
N = 7

# generating random strings using secrets.choice()
return_str = ''.join(secrets.choice(string.ascii_uppercase + string.digits)
                     for i in range(N))

# print result
print("The generated random string : " + str(return_str))

Output
The generated random string : 26SJ8IA

 

Method 3: Using random.choices()

The third approach is by random.choices. The choices() function returns a collection of random elements with replacement.

Python
import random
import string

# initializing empty string
return_str = ""
  
# initializing size of string
N = 7

# generating random strings using random.choices()
return_str = ''.join(random.choices(
    string.ascii_uppercase + string.digits, k=N))

# print result
print("The generated random string : " + str(return_str))

Output
The generated random string : H4ICGAZ

Method 4: Using secrets module

 step-by-step algorithm for implementing this approach:

  1. Import the secrets and string modules.
  2. Define the id_generator function with a single argument, length.
  3. Initialize an empty string, return_str.
  4. Generate a string containing uppercase letters and digits (0-9) using string.ascii_uppercase and the string literal '0123456789'.
  5. Use a list comprehension to generate a list of length random characters from the data string using secrets.choice(data).
  6. Use the join method to concatenate the characters in the list into a single string.
  7. Assign the resulting string to return_str.
  8. Print the generated random string with a message.
  9. Return the generated string.
Python
import secrets
import string

def id_generator(length):

    # initializing empty string
    return_str = ""

    # generating a string containing A-Z and 0-9
    data = string.ascii_uppercase + '0123456789'

    # using the secrets module to generate the random string
    return_str = ''.join(secrets.choice(data) for i in range(length))

    # print result
    print("The generated random string : " + str(return_str))

# function call
id_generator(7)

Output
The generated random string : YB7HXVH

The time complexity of this algorithm is O(n), where n is the length of the string to be generated. The secrets.choice() method is called n times to generate the random characters, and the join() method is called once to concatenate the characters into a string.

The auxiliary space of this algorithm is also O(n), where n is the length of the string to be generated. This is because the function creates a list of length n to hold the random characters, and then concatenates them into a string of length n. The space used by the data string and the return_str variable is constant, regardless of the length of the generated string.


Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy