[英]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:我正在研究 python 中的 Discord 机器人,我需要一种生成随机十六进制颜色代码的方法,我在网上查了一下,发现了这个:
import random
color = "%06x" % random.randint(0, 0xFFFFFF)
Is it possible to do the same thing using an f-string?是否可以使用 f 字符串做同样的事情?
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!但这只是一种偏好!
Yes, you can use:是的,您可以使用:
import random
color = f'{"%06x" % random.randint(0, 0xFFFFFF)}'
Let me know if this is what you are trying to achieve.让我知道这是否是您想要实现的目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.