I'm new to Python. How can I generate this code with a different or unequal character?
I want output like this
41PVY02KF#
83#YCF6X15
import random
import string
FullChar = CEFLMPRTVWXYK0123456789#
count = 10
count = int(count)
UniqueCode = 0
for i in range(count):
UniqueCode += random.choice(FullChar)
print(UniqueCode)
Assuming you will have all possible characters in full_char
, count
is the length of your unique_code
, you can use:
import random
full_char = 'CEFLMPRTVWXYK0123456789#'
count = 10
unique_code = ''.join(random.sample(full_char, count))
This code will generate a code of random characters that are unique from full_char
.
If you always want to have a '#'
, you can use the following:
import random
full_char = 'CEFLMPRTVWXYK0123456789'
count = 10
unique_code = ''.join(random.sample(full_char, count - 1)) + '#'
unique_code = ''.join(random.sample(unique_code, len(unique_code)))