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 this post
''.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'
Answer from Ignacio Vazquez-Abrams on Stack OverflowAnswer 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 this post
''.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'
This Stack Overflow quesion is the current top Google result for "random string Python". The current top answer is:
''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
This is an excellent method, but the PRNG in random is not cryptographically secure. I assume many people researching this question will want to generate random strings for encryption or passwords. 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))
Using random.SystemRandom() instead of just random uses /dev/urandom on *nix machines and CryptGenRandom() in Windows. These are cryptographically secure PRNGs. 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.
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))
The module docs also discuss convenient ways to generate secure tokens and best practices.
python - How to generate a random string with symbols - Stack Overflow
python - Generating a random string of characters and symbols - Code Review Stack Exchange
how can i generate random strings of specific length?
Python code that makes a word with random numbers and character in last
What are some advanced techniques for generating random strings in Python, and when should I use them?
What is a random string, and why is it important in programming?
How can I store and manage random strings securely to prevent exposure?
Videos
How about:
import random
import string
random = ''.join([random.choice(string.ascii_letters + string.digits + string.punctuation ) for n in range(12)])
Try:
import random
import string
random = ''.join([random.choice(string.ascii_letters + string.digits ) for n in
range(12)])
print(random)`
Python already defines a number of strings of possible characters. See string.ascii_lowercase and string.digits Source
I would use True and False instead of "yes"/"uppercase" as arguments to generator(). This function might be used by code that does directly interact with a user, so passing a string would not make sense. Additionally, the restart prompt supports a number of positive responses that are not supported by this function. You should have one layer that controls interaction with the user and one that generates a password. This will make the function cleaner as well as an easier API to work with.
Two more point about separation of concerns with generator():
It should return the password instead of printing it. Again, this allows the function to be used when not directly interacting with a command prompt.
It should throw an exception instance of calling
sys.exit().exit()will stop the python process and not allow any more execution of code. Your code is written so that multiple passwords can be generated one after another. However, if the user accidentally enters an invalid character to the first question, the application stops running instead asking the user to input a correct value. Throwing an exception would have the same result if you don't change the rest of your code, but allows you to change the code that interacts with the user to handle this case without restarting the application.
You convert length to an integer repeatedly instead of storing the value. The first validation completely ignores the result and the while loop does the conversion every time it tests if it should continue looping. This can all be solved by having length be passed in as an integer and letting the user layer handle the conversion and error cases.
Generator is already a well defined term with in Python that do something very different. The function should be renamed to generate_password() or something similar.
You are still not following the advice offered in previous answers to your questions.
Quoting from Edward's great answer on "Are there any bugs or ways to make my divisor code better?":
Separate I/O from calculation
The main purpose of the program is to factor numbers into divisors which is something that is potentially reusable. Both to make it more clear as to what the program is doing and to allow for future re-use, I'd suggest extracting the factoring part into a separate function and the have the input from the user be done within the main routine or some other input-only function.
Your password generating function, sadly, asks the user for input and thus can never be re-used outside the context of this application. Instead, make it take exactly two arguments as the only inputs: a string containing the characters to base the password on, and the length of the password:
def make_password(length, alphabet):
return ''.join(random.choice(alphabet) for _ in range(length))
Here, random.choice(alphabet) replaces merge[random.randint(0, len(merge) - 1)]
in a more readable way.
Another repeat problem, quoting from the same answer:
Think of your user
Right now, the user has to enter "yes" or the equivalent and then enter another number to be factored. However, the prompt doesn't suggest that "y" is a valid answer. Adding that to the prompt would help the user understand what the computer is expecting. Better still, would be t eliminate that question and to simply ask for the next number with a number '0' being the user's way to specify no more numbers. Of course the prompt should be changed to tell the user of this fact.
Your user interface is basically the same: it would drive anyone who used it regularly up the wall. This is a command-line program; throw away all the input prompting and give it a command-line argument interface instead:
usage: makepwd.py [-h] [-d] [-s] [-l | -u] length Generates passwords of the specified length, optionally including digits and/or symbols. positional arguments: length a positive integer denoting the password length optional arguments: -h, --help show this help message and exit -d, --digits include digits in the generated password -s, --symbols include symbols in the generated password -l, --lower use only lowercase letters -u, --upper use only uppercase letters
The argparse module can take care of this for you, but you need to familiarize yourself with its semantics to understand what is going on. Let's take a look at every part individually:
def parse_args():
parser = argparse.ArgumentParser(description=__doc__, argument_default='')
parser.set_defaults(letters=string.ascii_letters)
We've created an argument parser and provided it the docstring of our module to use a description. In your original code, lower-case characters were used as a default. This is a bad default, instead use both lower and upper case if nothing else is specified.
parser.add_argument('length', type=int,
help='a positive integer denoting the password length')
The first argument we need is the length of the password. argparse will convert the value to an int and take care of the error handling for us.
add_const_arg = arg_adding_function_for(parser)
add_const_arg('-d', '--digits', const=string.digits,
help='include digits in the generated password')
add_const_arg('-s', '--symbols', const='#*ยฃ$+-.',
help='include symbols in the generated password')
Our next two arguments determine the non-alphabetical characters to include in the password.
group = parser.add_mutually_exclusive_group()
store_letters = arg_adding_function_for(group, dest='letters')
store_letters('-l', '--lower', const=string.ascii_lowercase,
help='use only lowercase letters')
store_letters('-u', '--upper', const=string.ascii_uppercase,
help='use only uppercase letters')
And the final arguments are for overriding the mixed-case default, so it's possible to generate a password containing only lower-case or only upper-case letters (in addition to the digits and symbols). These arguments are mutually exclusive: a password can not be upper-cased and lower-cased at the same time.
return parser.parse_args()
And that's it. Almost. You may have noticed I didn't explain the arg_adding_function_for function yet. I defined it as the following higher-order helper function to simplify the above code by using functools.partial to pre-set some of the options that are common for each argument. (For flexibility, the *args parameter is included, though not technically necessary - find out more about *args and **kwargs).
def arg_adding_function_for(parser, *args, action='store_const', **kwargs):
return functools.partial(parser.add_argument, action=action, *args, **kwargs)
The whole thing in once piece:
"""
Generates passwords of the specified length, optionally including digits and/or symbols.
"""
import argparse
import functools
import random
import string
def main():
args = parse_args()
password = make_password(args.length, args.letters + args.digits + args.symbols)
print(password)
def make_password(length, alphabet):
return ''.join(random.choice(alphabet) for _ in range(length))
def parse_args():
parser = argparse.ArgumentParser(description=__doc__, argument_default='')
parser.set_defaults(letters=string.ascii_letters)
parser.add_argument('length', type=int,
help='a positive integer denoting the password length')
add_const_arg = arg_adding_function_for(parser)
add_const_arg('-d', '--digits', const=string.digits,
help='include digits in the generated password')
add_const_arg('-s', '--symbols', const='#*ยฃ$+-.',
help='include symbols in the generated password')
group = parser.add_mutually_exclusive_group()
store_letters = arg_adding_function_for(group, dest='letters')
store_letters('-l', '--lower', const=string.ascii_lowercase,
help='use only lowercase letters')
store_letters('-u', '--upper', const=string.ascii_uppercase,
help='use only uppercase letters')
return parser.parse_args()
def arg_adding_function_for(parser, *args, action='store_const', **kwargs):
return functools.partial(parser.add_argument, action=action, *args, **kwargs)
if __name__ == '__main__':
main()
how can i generate random strings of specific length (100) & specific characters (no numbers but all alphabet in upper case and lowercase and _underscore and dash -?????
thanks
ยป pip install StringGenerator
Hello, I am new to the world of programming. I want to create a word that basically from three letters and combines with at least 2 numbers or special characters in the last of the word meaning a word that starts with three letters and ends with random numbers that do not exceed two numbers,
i don't know how to do it.
i just found this but i still cannot fix to what i need
import random
from string import ascii_lowercase, ascii_uppercase, digits
for _ in range(2):
fill_letter = random.choice(ascii_lowercase)
fill_number = random.choice(digits)
choices = (fill_letter,fill_number)
word = 'hkr'.join(random.choices(choices, k=2))
print(word)