0

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)
    
S Raghav
  • 1,386
  • 1
  • 16
  • 26
here
  • 25
  • 5
  • 1
    Possible duplicate of [Random string generation with upper case letters and digits](https://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits) – BenT Apr 01 '19 at 04:16
  • @BenT, I think is not a duplication because he wants unique characters from a string, but in that question you make a random choice which implies some characters might repeat. – lmiguelvargasf Apr 01 '19 at 04:37

1 Answers1

0

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)))
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228