Random string generation with upper case letters and digits

前端 未结 30 3070
逝去的感伤
逝去的感伤 2020-11-22 02:51

I want to generate a string of size N.

It should be made up of numbers and uppercase English letters such as:

  • 6U1S75
  • 4Z4UKK
  • U911K4
相关标签:
30条回答
  • 2020-11-22 02:52

    Answer in one line:

    ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
    

    or even shorter starting with Python 3.6 using random.choices():

    ''.join(random.choices(string.ascii_uppercase + string.digits, k=N))
    

    A cryptographically more secure version; see https://stackoverflow.com/a/23728630/2213647:

    ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(N))
    

    In details, with a clean function for further reuse:

    >>> import string
    >>> import random
    >>> def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    ...    return ''.join(random.choice(chars) for _ in range(size))
    ...
    >>> id_generator()
    'G5G74W'
    >>> id_generator(3, "6793YUIO")
    'Y3U'
    

    How does it work ?

    We import string, a module that contains sequences of common ASCII characters, and random, a module that deals with random generation.

    string.ascii_uppercase + string.digits just concatenates the list of characters representing uppercase ASCII chars and digits:

    >>> string.ascii_uppercase
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    >>> string.digits
    '0123456789'
    >>> string.ascii_uppercase + string.digits
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    

    Then we use a list comprehension to create a list of 'n' elements:

    >>> range(4) # range create a list of 'n' numbers
    [0, 1, 2, 3]
    >>> ['elem' for _ in range(4)] # we use range to create 4 times 'elem'
    ['elem', 'elem', 'elem', 'elem']
    

    In the example above, we use [ to create the list, but we don't in the id_generator function so Python doesn't create the list in memory, but generates the elements on the fly, one by one (more about this here).

    Instead of asking to create 'n' times the string elem, we will ask Python to create 'n' times a random character, picked from a sequence of characters:

    >>> random.choice("abcde")
    'a'
    >>> random.choice("abcde")
    'd'
    >>> random.choice("abcde")
    'b'
    

    Therefore random.choice(chars) for _ in range(size) really is creating a sequence of size characters. Characters that are randomly picked from chars:

    >>> [random.choice('abcde') for _ in range(3)]
    ['a', 'b', 'b']
    >>> [random.choice('abcde') for _ in range(3)]
    ['e', 'b', 'e']
    >>> [random.choice('abcde') for _ in range(3)]
    ['d', 'a', 'c']
    

    Then we just join them with an empty string so the sequence becomes a string:

    >>> ''.join(['a', 'b', 'b'])
    'abb'
    >>> [random.choice('abcde') for _ in range(3)]
    ['d', 'c', 'b']
    >>> ''.join(random.choice('abcde') for _ in range(3))
    'dac'
    
    0 讨论(0)
  • 2020-11-22 02:52

    Based on another Stack Overflow answer, Most lightweight way to create a random string and a random hexadecimal number, a better version than the accepted answer would be:

    ('%06x' % random.randrange(16**6)).upper()
    

    much faster.

    0 讨论(0)
  • 2020-11-22 02:52

    you can now use a new library (python >= 3.6) chancepy here

    from chancepy import Chance
    
    random_string = Chance.string(length=10, pool="someLettersAndNumbers123")
    
    0 讨论(0)
  • 2020-11-22 02:53

    Sometimes 0 (zero) & O (letter O) can be confusing. So I use

    import uuid
    uuid.uuid4().hex[:6].upper().replace('0','X').replace('O','Y')
    
    0 讨论(0)
  • 2020-11-22 02:54

    this is a take on Anurag Uniyal 's response and something that i was working on myself.

    import random
    import string
    
    oneFile = open('‪Numbers.txt', 'w')
    userInput = 0
    key_count = 0
    value_count = 0
    chars = string.ascii_uppercase + string.digits + string.punctuation
    
    for userInput in range(int(input('How many 12 digit keys do you want?'))):
        while key_count <= userInput:
            key_count += 1
            number = random.randint(1, 999)
            key = number
    
            text = str(key) + ": " + str(''.join(random.sample(chars*6, 12)))
            oneFile.write(text + "\n")
    oneFile.close()
    
    0 讨论(0)
  • 2020-11-22 02:55
    import string
    from random import *
    characters = string.ascii_letters + string.punctuation  + string.digits
    password =  "".join(choice(characters) for x in range(randint(8, 16)))
    print password
    
    0 讨论(0)
提交回复
热议问题