Generate Random Number and String in Python

Creating random number or string in Python is way more easier then you think. random module can be used to generate random numbers in Python. Combining String module with random module, we can generate random strings in Python.

I have tried these two modules myself in multiple programs to generate random numbers and strings. we will see with a code examples, how we can use in-built Python libraries to generate random numbers and strings.

We will be using following constants from string module in our code example.

Module String constants

  • string.ascii_letters : Using this constant will produce a string combination of upper and lower case.
# usage
''.join(random.choice(string.ascii_letters) for i in range(8))
  • string.ascii_lowercase : It produces a random string containing only lower case characters.
# usage
''.join(random.choice(string.ascii_lowercase) for i in range(8))
  • string.ascii_uppercase : It produces a random string containing only upper case characters.
# usage
''.join(random.choice(string.ascii_uppercase) for i in range(8))
  • string.digits : It produces a random string containing 0 to 9 repeating integer value. You can use random.sample() to generate string of non repeating numeric characters. Further you can convert the string to integer.
    Although you can use randint to achieve same functionality. The advantage of using randint is that it generates pure random integer and you need not to be worrying about converting it to integer.
# usage
''.join(random.choice(string.digit) for i in range(8))

# using randint
import randint from random
randint(10000000,99999999)
  • string.hexdigits : It produces a random string containing hex digits (0123456789abcdefABCDEF).
    If you are working on some sort of hash capability, the following code is very handy for you. You can use it to generate md5, sha1, sha256 hashes.
# usage
''.join(random.choice(string.ascii_hexdigits) for i in range(8))
  • .string.octdigits: It produces a random string containing oct digits (01234567)
# usage
''.join(random.choice(string.octdigits) for i in range(8))
  • .string.punctuation: It produces a random string containing non alphabetical characters also called special characters.
# usage
''.join(random.choice(string.punctuation) for i in range(8))
  • string.printable : It produces a random string containing alphabetical characters and special characters including space.
# usage
''.join(random.choice(string.string.punctuation) for i in range(8))

Here is the complete code in Python to generate random numbers and strings.

## random string with 16 char containing only ascii letters.
''.join(random.choice(string.ascii_letters) for i in range(16))

## random string with 16 char containing only ascii lower letters.
''.join(random.choice(string.ascii_lowercase) for i in range(16))

## random string with 16 char containing only ascii upper letters.
''.join(random.choice(string.ascii_uppercase) for i in range(16))

## random string with 16 char containing only digits.
''.join(random.choice(string.digit) for i in range(16))

## random string with 16 char containing only hexa decimal characters.
''.join(random.choice(string.ascii_hexdigits) for i in range(16))

## random string with 16 char containing only octa decimal characters.
''.join(random.choice(string.octdigits) for i in range(16))

## random string with 16 char containing only special characters.
''.join(random.choice(string.punctuation) for i in range(16))

## random string with 16 char containing all printable characters.
''.join(random.choice(string.string.printable) for i in range(16))