How to get uuid of 8 characters only? I did the code line below and got uuid of 36 characters.
id1 = str(uuid.uuid1())
How to get uuid of 8 characters only? I did the code line below and got uuid of 36 characters.
id1 = str(uuid.uuid1())
The correct answer to this question, as answered here is that it is impossible to generate an 8 character uuid, because uuids are 16 bytes by definition.
The standard for UUIDs is specified in RFC 4221. In the format section, the first words are:
The UUID format is 16 octets
Thank you shudipta-sharma for noting that my original answer would NOT produce a universally unique id.
You could just take the first eight characters.
No guarantees about them all being unique.
You can try this:
import string, random
# ''.join() joins the letters from random.choices() into a single Python str of length k.
join=''.join
join(random.choices(string.ascii_letters, k=8)) # Output is like as 'XDCxVAJl'
Resource: Python’s string module contains a number of useful constants: ascii_lowercase, ascii_uppercase, string.punctuation, ascii_whitespace, string.hexdigits and a handful of others.
You can try this, assuming you only want the first 8 characters regardless of object type (python3):
import uuid
id1 = str(uuid.uuid1())
first_eight=id1[:8]
print(first_eight)