Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Answer in one line:</strong></p> <pre><code>''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N)) </code></pre> <p>or even shorter starting with Python 3.6 using <a href="https://docs.python.org/3/library/random.html#random.choices" rel="noreferrer"><code>random.choices()</code></a>:</p> <pre><code>''.join(random.choices(string.ascii_uppercase + string.digits, k=N)) </code></pre> <p><strong>A cryptographically more secure version; see <a href="https://stackoverflow.com/a/23728630/2213647">https://stackoverflow.com/a/23728630/2213647</a>:</strong></p> <pre><code>''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(N)) </code></pre> <p><strong>In details, with a clean function for further reuse:</strong></p> <pre><code>&gt;&gt;&gt; import string &gt;&gt;&gt; import random &gt;&gt;&gt; def id_generator(size=6, chars=string.ascii_uppercase + string.digits): ... return ''.join(random.choice(chars) for _ in range(size)) ... &gt;&gt;&gt; id_generator() 'G5G74W' &gt;&gt;&gt; id_generator(3, "6793YUIO") 'Y3U' </code></pre> <p><strong>How does it work ?</strong></p> <p>We import <code>string</code>, a module that contains sequences of common ASCII characters, and <code>random</code>, a module that deals with random generation.</p> <p><code>string.ascii_uppercase + string.digits</code> just concatenates the list of characters representing uppercase ASCII chars and digits:</p> <pre><code>&gt;&gt;&gt; string.ascii_uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' &gt;&gt;&gt; string.digits '0123456789' &gt;&gt;&gt; string.ascii_uppercase + string.digits 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' </code></pre> <p>Then we use a list comprehension to create a list of 'n' elements:</p> <pre><code>&gt;&gt;&gt; range(4) # range create a list of 'n' numbers [0, 1, 2, 3] &gt;&gt;&gt; ['elem' for _ in range(4)] # we use range to create 4 times 'elem' ['elem', 'elem', 'elem', 'elem'] </code></pre> <p>In the example above, we use <code>[</code> to create the list, but we don't in the <code>id_generator</code> function so Python doesn't create the list in memory, but generates the elements on the fly, one by one (more about this <a href="https://stackoverflow.com/questions/231767/the-python-yield-keyword-explained/231855#231855">here</a>).</p> <p>Instead of asking to create 'n' times the string <code>elem</code>, we will ask Python to create 'n' times a random character, picked from a sequence of characters:</p> <pre><code>&gt;&gt;&gt; random.choice("abcde") 'a' &gt;&gt;&gt; random.choice("abcde") 'd' &gt;&gt;&gt; random.choice("abcde") 'b' </code></pre> <p>Therefore <code>random.choice(chars) for _ in range(size)</code> really is creating a sequence of <code>size</code> characters. Characters that are randomly picked from <code>chars</code>:</p> <pre><code>&gt;&gt;&gt; [random.choice('abcde') for _ in range(3)] ['a', 'b', 'b'] &gt;&gt;&gt; [random.choice('abcde') for _ in range(3)] ['e', 'b', 'e'] &gt;&gt;&gt; [random.choice('abcde') for _ in range(3)] ['d', 'a', 'c'] </code></pre> <p>Then we just join them with an empty string so the sequence becomes a string:</p> <pre><code>&gt;&gt;&gt; ''.join(['a', 'b', 'b']) 'abb' &gt;&gt;&gt; [random.choice('abcde') for _ in range(3)] ['d', 'c', 'b'] &gt;&gt;&gt; ''.join(random.choice('abcde') for _ in range(3)) 'dac' </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload