Hyphenate a random string to an exact format
Posted
by chrissygormley
on Stack Overflow
See other posts from Stack Overflow
or by chrissygormley
Published on 2010-03-31T10:21:44Z
Indexed on
2010/03/31
10:33 UTC
Read the original article
Hit count: 363
python
|string-manipulation
Hello,
I am creating a random ID using the below code:
from random import *
import string
# The characters to make up the random password
chars = string.ascii_letters + string.digits
def random_password():
return "".join(choice(chars) for x in range(32))
This will output something like:
60ff612332b741508bc4432e34ec1d3e
I would like the format to be in this format:
60ff6123-32b7-4150-8bc4-432e34ec1d3e
I was looking at the .split()
method but can't see how to do this with a random id, also the hyphen's must be at these places so splitting them by a certain amount of digits is out. I'm asking is there a way to split these random id's by 8 number's then 4 etc.
Thanks
© Stack Overflow or respective owner