One compact way to do this is to use a list of concepts (which are a specific type of loop):
>>> alpha = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F") >>> ''.join([random.choice(alpha) for _ in range(6)]) '4CFDE4'
You can shorten the alphabet string with range and map :
>>> alpha = map(str, range(10)) + ["A", "B", "C", "D", "E", "F"]
Or just just use the line:
>>> alpha = "ABCDEF0123456789"
PS. Since colors are hexadecimal, why not just create a random number and translate it into hexadecimal?
>>> hex(random.randint(0, 16777215))[2:].upper() 'FDFD4C'
miku Nov 08 '13 at 1:07 2013-11-08 01:07
source share