Python: Generate a random color hex, alphabetical string, random value and multiple of 7

Python module: Exercise-1 with Solution

Write a Python program to generate a random color hex, a random alphabetical string, random value between two integers (inclusive) and a random multiple of 7 between 0 and 70.

Use random.randint()

Sample Solution:

Python Code:

import random
import string
print("Generate a random color hex:")
print("#{:06x}".format(random.randint(0, 0xFFFFFF)))
print("\nGenerate a random alphabetical string:")
max_length = 255
s = ""
for i in range(random.randint(1, max_length)):
    s += random.choice(string.ascii_letters)
print("Generate a random value between two integers, inclusive:")
print(random.randint(0, 10))
print(random.randint(-7, 7))
print(random.randint(1, 1))
print("Generate a random multiple of 7 between 0 and 70:")
print(random.randint(0, 10) * 7)

Sample Output:

Generate a random color hex:

Generate a random alphabetical string:
Generate a random value between two integers, inclusive:
Generate a random multiple of 7 between 0 and 70:


Flowchart: Generate a random color hex, alphabetical string, random value and random multiple of 7

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Python module methods Exercise Home.
Next: Write a Python program to select a random element from a list, set, dictionary (value) and a file from a directory.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Inverts a dictionary with non-unique hashable values:


def tips_collect_dictionary(obj):
  inv_obj = {}
  for key, value in obj.items():
    inv_obj.setdefault(value, list()).append(key)
  return inv_obj
ages = {
  "Owen": 25,
  "Jhon": 25,
  "Pepe": 15,


{25: ['Owen', 'Jhon'], 15: ['Pepe']}


We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook