It's on PyPI: pip install StringGeneratorĭisclosure: I'm the author of the strgen module. The various attempts to generalize a solution all have limitations that strgen solves with greater brevity and expressive power using a simple template language. This all requires lots more code than in the answers provided. You will want to use SystemRandom (or fallback if not available), make sure required character sets are represented, use unicode (or not), make sure successive invocations produce a unique string, use a subset of one of the string module character classes, etc. But you will hardly ever use it in that form. The solution from Ignacio is the fastest run-time performing and is the right answer using the Python Standard Library. Strgen is faster in developer-time than any of the above solutions. Might not have a digit (or uppercase character) in it. We need to be aware that this: ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N)) Generate a 6-character random string with upper case letters and digits: > from strgen import StringGenerator as SG The module docs also discuss convenient ways to generate secure tokens and best practices.Ī faster, easier and more flexible way to do this is to use the strgen module ( pip install StringGenerator). If you're using python3.6 or above, you can use the new secrets module as mentioned in MSeifert's answer: ''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(N)) Using random.choice instead of random.SystemRandom().choice in an application that requires a secure PRNG could be potentially devastating, and given the popularity of this question, I bet that mistake has been made many times already. These are cryptographically secure PRNGs. Using random.SystemRandom() instead of just random uses /dev/urandom on *nix machines and CryptGenRandom() in Windows. You can do this securely by making a small change in the above code: ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(N)) I assume many people researching this question will want to generate random strings for encryption or passwords. This is an excellent method, but the PRNG in random is not cryptographically secure. The current top answer is: ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N)) This Stack Overflow quesion is the current top Google result for "random string Python". > ''.join(random.choice('abcde') for _ in range(3)) Then we just join them with an empty string so the sequence becomes a string: > ''.join() Characters that are randomly picked from chars: > Therefore random.choice(chars) for _ in range(size) really is creating a sequence of size characters. 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") 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). > # we use range to create 4 times 'elem' Then we use a list comprehension to create a list of 'n' elements: > range(4) # range create a list of 'n' numbers String.ascii_uppercase + string.digits just concatenates the list of characters representing uppercase ASCII chars and digits: > string.ascii_uppercase We import string, a module that contains sequences of common ASCII characters, and random, a module that deals with random generation. return ''.join(random.choice(chars) for _ in range(size)) > def id_generator(size=6, chars=string.ascii_uppercase + string.digits): In details, with a clean function for further reuse: > import string Or even shorter starting with Python 3.6 using random.choices(): ''.join(random.choices(string.ascii_uppercase + string.digits, k=N))Ī cryptographically more secure version: see this post ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(N)) Answer in one line: ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
0 Comments
Leave a Reply. |