×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Random string generation with upper case letters and digits in Python

Last Updated : April 28, 2025

Generating random strings containing uppercase letters and digits is a common requirement for creating unique IDs, passwords, and tokens. Python provides several ways to generate such random strings.

In this chapter, we will explore multiple approaches to generate random strings with uppercase letters and digits.

Approach 1: Using random.choices()

You can generate a random string using the random.choices() method along with the string module to select random characters.

Example

This example demonstrates using random.choices() for random string generation.

import random
import string

def generate_random_string(length):
    characters = string.ascii_uppercase + string.digits
    return ''.join(random.choices(characters, k=length))

# Generate a random string of length 8
print(generate_random_string(8))

The output of the above code would be:

Example output: 7G5D2B9Q

Approach 2: Using random.choice() in a Loop

You can build a random string by selecting one character at a time using a loop with random.choice().

Example

This example demonstrates generating a random string by picking characters one by one.

import random
import string

def generate_random_string(length):
    characters = string.ascii_uppercase + string.digits
    result = ''
    for _ in range(length):
        result += random.choice(characters)
    return result

# Generate a random string of length 10
print(generate_random_string(10))

The output of the above code would be:

Example output: H2K9L8JM3Q

Approach 3: Using secrets.choice() for Cryptographic Security

You can use the secrets module for generating random strings when better cryptographic security is required.

Example

This example demonstrates using the secrets module for secure random string generation.

import secrets
import string

def generate_random_string(length):
    characters = string.ascii_uppercase + string.digits
    return ''.join(secrets.choice(characters) for _ in range(length))

# Generate a secure random string of length 12
print(generate_random_string(12))

The output of the above code would be:

Example output: Q9F4G2B7Z1XT

Approach 4: Using numpy.random.choice()

You can use numpy.random.choice() if you are already working with NumPy arrays and need high performance.

Example

This example demonstrates using numpy.random.choice() for generating a random string efficiently.

import numpy as np

def generate_random_string(length):
    characters = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
    return ''.join(np.random.choice(characters, size=length))

# Generate a random string of length 6
print(generate_random_string(6))

The output of the above code would be:

Example output: B3Z8F2

Exercise

Select the correct option to complete each statement about random string generation with uppercase letters and digits in Python.

  1. The ___ module in Python is commonly used to generate random choices.
  2. The ___ module provides constants like ascii_uppercase and digits.
  3. To generate a random string, you can use random.choices() by combining string.ascii_uppercase and string.___.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.