Welcome back in the previous lesson,
we successfully learned how to generate a random letter
by making use of the random module. And also the random dot choice method
a question I have here is, well, what if we wanted to generate multiple letters?
And not just one,
what we have to do is to, first of all, change the method from random dot choice
to random dot choices
because now it's plural, we're dealing with more than one letter.
Another thing about random dot choices as a method of function
is that it's going to accept two parameters instead of one.
The first will be
what we call the population,
which is basically where we're pulling the letters from.
And then the second
is going to be the actual number
of letters we want to generate.
So in this case, right now, I'm going to say K equals three.
All right. And now I can change the print text from
letter to letters and then change it to R.
Now, if I run the program, we have K RJ, I can run it again. We have QMW. Right.
So you can keep that in mind.
However,
we want to move a step further to generate a random password.
So we're no longer just dealing with letters.
We're now going to combine letters with uppercase and numbers and so on.
And I'm pretty sure you've seen random password generators before.
Maybe it's on the websites where you are asked to create an account.
You want to create an account and then they offer to generate a password for you,
which you can then change later.
I'm gonna show you how we can create our random password generator.
And we're just gonna use five lines to do
this.
In fact, four lines, just four lines. OK? You don't believe me, take a look at this.
So first of all,
we're going to keep the import random because obviously
we want to randomize the letters and numbers.
But we're gonna import
a library here called string
because we're gonna be manipulating our strings which will include letters,
numbers and so on.
So we have import string
and now I'm going to create a variable that will represent the password.
I'm gonna call it password
equals.
And I remember from the previous lesson, I'm gonna say random.
OK
dot Choi says,
because now we're going to be dealing with multiple letters
and now in brackets, remember the first parameter for this function or method
is going to be the population.
We have to tell the function where to pull the string from the letters from.
The thing about the string
library
is that let me just show you
over here the string module. But
we have a variety of methods or in this case, we call them constants.
So we can say string
key underscore letters, this will pull in letters
that are both capital, lower case.
And then we can specify, we can say string
key, lower case, this will pull in only lower case letters.
And then we have for upper case, we have for digits,
digits, punctuation
and so on. So we have all these methods, all this constant that we can work with
when importing the string module. So
let me drag this one away.
What we're gonna do right now is for the random choices, we want to pull
string
dot
as
key
and now underscore letters,
ok? We're gonna pull random
letters.
And then in addition, plus
we also want to pull in what digits
we want to have numbers
in our password as well.
So basically we're telling the function we're telling Python that hey,
for the choices of letters
for the password, we're gonna pull in letters that are both uppercase and lowercase
and then also digits as well.
And then of course,
the second parameter we need to specify how many
letters digits are going to be in our password.
So I'm gonna say K
equals and let's go with 10.
Ok? So a pretty strong password
and now all we have to do is what just print,
we're gonna print
and I'm gonna say uh
generated
so generated password.
Ok.
And then
let me add my comma and then password,
that's it.
We're going to generate a password
and now let's try it. Ok. So I'm going to go ahead right now, run the program
and there you go.
We have capital K,
we have six, we have capital F, we have 60, small letter
H, capital J zero, capital W and then capital R.
So you can see right now, it successfully created
it passed with 10 characters. We had upper case letters, lower case letters
and a few numbers as well.
But
what if, what if instead
of having the characters separated by a comma and codes,
we want to join everything together.
So we have just one single string.
How are we going to do this?
We will have to make use of a special function called
the join
function.
It's actually a method.
So
what we want to do right now is
over here where it says random
choices.
We're going to add this very special
function
called join and it goes like this,
we're gonna have
a single codes and then dots
join. And now we'll have to open up a bracket to
cover everything in here
and there it is.
So what this is going to do is that it's going to generate the password,
but then all the characters will be joined together.
Now, if I run the program one more time,
there you go.
Now we have it looking much better. I can run it again
and there you go,
run it one more time
and there you go.
So it's kind of amazing how with just four lines of code,
we've been able to create our very own
random password generator.
We of course had to make use of two
standard modules, the
random module and then the string module and of course
two additional lines of code
and there it is.
So that's it. Thank you for watching the video. I will see you in the next class.