W3docs

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:

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

Random string generation with upper case letters and digits in Python

python— editable, runs on the server

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:

Random string generation with upper case letters and digits in Python using the random.sample method

python— editable, runs on the server

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.