pythongenerated-code

How to generated code with different or unequal characters in python


I'm new to Python. How can I generate this code with a different or unequal character?

I want output like this


Solution

  • 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)))