Search code examples
pythonpython-3.xdiscorddiscord.pyf-string

How can I generate a random hex color code using python?


I am working on a Discord bot in python, and I need a way to generate a random hex color code, I looked it up online and I found this:

import random
color = "%06x" % random.randint(0, 0xFFFFFF)

Is it possible to do the same thing using an f-string?


Solution

  • import random
    color = f"{"%06x" % random.randint(0, 0xFFFFFF)}"
    

    Is this what you want?

    However, I don't think this is a good way to create a hex code. Personally, I would do it like this:

    import random
    random_number = random.randint(0,16777215)
    hex_number = str(hex(random_number))
    hex_number ='#'+ hex_number[2:]
    

    But that's just a preferance!