Generate Random string (letters & digits) in Python

In this article, we will learn to generate a random string with upper case letters and digits in Python. Also we will be learning about ASCII encoding format which will be used in one of the method to generate a random string with upper case letters and digits.

Table Of Contents

What is ASCII?

ASCII Stands for American Standard Code for Information Interchange. It is the most used character encoding format. It is a 7-bit character code in which every single bit represents a unique character.

Every character in english alphabet has a unique ASCII code,

  • ASCII code of A to Z (Upper Case) starts from 065 and ends at 090.
  • ASCII code of a to z (Lower Case) starts from 097 and ends at 122.

Now we will look at the methods through which we can genarate random string with upper case letters and digits.

Generate Random string with Uppercase letters and digits using random.choice()

First method we will be using to create a random string of combination of upper case letters and digits is the choice() method of random module, which comes bundled with Python. We will use string.ascii_uppercase() and string.digits() functions of string method to genarate random alphabets and digits respectively. At last we will use join() method to join all the chars and digits genarated with random.choice() method. Let’s see this Example.

EXAMPLE :

import string
import random

# initialized a empty list in which 
# random string will be appended.
randomcharlst = [] 

# Arguments in range() will print desired length of
# string (uppercase alphabets + digits).
# Here length should be of 10 chars.
for i in range(0,10):
    randomcharlst.append( random.choice(string.ascii_uppercase + string.digits) )

randomStr = ''.join(randomcharlst)

print(randomStr)

OUTPUT :

1YRBJO9D2K

As you can see in above example a combination of upper case alphabets and digits has been created and stored in randomStr. Every time you run this code it will print a unique combination.

Generate Random string with Uppercase letters and digits Using random.choices()

Next method we can use to generate a random string with upper case letters and digits is the choices() method of random module. This method is same as choice() method with one difference. It recives a second parameter k, which denotes the length of the string. Also we will use same approach as used in Method 1 but this time we will not use range function. Instead we will pass second argument k for the length of string. Let’s see this Example.

EXAMPLE :

import string
import random

# Generate random string of 10 characters including only uppercase letters and digits
randomStr = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))

print(randomStr)

OUTPUT :

NEFJ2V5DU9

As you can see in code above we have used random.choices() method to generate a random string with upper case letters and digits. A combination of upper case alphabets and digits has been created using string.ascii_uppercase() and string.digits() function and it has been passed as the first argument in random.choices(). This method helps us to create a random string with a combination of digits and alphabets in a single line of code. Every time we will run this code, it will print a unique string of size 10.

Summary

So in this article we learned to generate a random string with upper case letters and digits using Python Programming Language. We basically learned about ASCII and two methods of same module thorugh which we can do our job. Both methods uses simmilar approcah and have only minor difference.
In method one (random.choice()) we use for loop for our desired length and in method 2 (random.choices()) we pass a second argument k which denotes the length of the string. Method 2 can be most useful and easy because it has shorter syntax and we just need to pass second argument for desired outcome. One other method that we can use is random.SystemRandom().choice() which is used for more cryptographically strong string because it has secure PRNG .

Make sure to read and understand the code and always run the code on your machine. Also we have used Python 3.10.1 for writing example codes. To check your version write python –version in your terminal.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top