Random string generation with upper case letters and digits

Here is a simple function that generates a random string of a given length using upper case letters and digits:

import random
import string

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

# Example
print(generate_random_string(8))

This will generate a random string of length 8, containing upper case letters and digits. You can adjust the length parameter to generate strings of different lengths.

You can also use the random.sample function to achieve the same result:

import random
import string

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

# Example
print(generate_random_string(8))

Both of these approaches use the string module's ascii_uppercase and digits constants to generate the random strings. The ascii_uppercase constant contains all upper case letters, and the digits constant contains all digits.