Welcome back.
So let's continue talking about the different types of methods or functions
that we typically apply on strings and one very common, one, in fact,
two very common ones rather
would be the functions for converting text either to upper case or lower case. So
in my example, over here, I have my variable text equal to cybersecurity.
If I wanted to convert everything to uppercase,
I would simply say text and now dot
upper
open up new brackets.
And there it is.
And if I wanted to convert everything to lowercase, I'll do the exact opposite,
this will be print text dot lower
dot lower,
open up new brackets and there it is. So if I run the program right now,
you can see we have cybersecurity in capital letters
and then cybersecurity in lower case letter.
So these are two new functions you should be aware of dot Oper
dot lower,
but there is also the function for trimming white space.
So say for example, I have my password variable in here, I've got space
and my actual password is swordfish. And then let me add some more space
if I wanted to trim all the unnecessary white space in here.
All I would do is I would say prints brackets and then password.
And now strip, once again
the strip function, we can also use it
to remove any unnecessary white space,
run the program. And there you go, swordfish without the empty space.
But we can also
replace characters
in our strings.
Say, for example, let me just give you an example in here.
Let's say I've got my message being equal to
and let's say, ok, user password.
Ok.
And let's say the password right now is password 123.
Ok,
just as an example, right?
But then we wanted to print something that will say, ok,
use a password and then redact it.
Ok? We don't want to display the actual password itself.
It's similar to what we did in the previous lesson
where we use the asterisk symbol to replace the characters.
It's related but slightly different. So let me show you what I'm talking about. Ok.
So I'm gonna come over here right now
and I'm gonna say
let's create a new variable, let's say secure
underscore message, ok,
will be equal to now message dot And now a new function called replace.
Ok,
I want to replace the actual password with something else. So
inside of my brackets,
I am now going to put the actual password I want to replace, which is password.
Uh 123
comma and now the actual letters or characters or string
I want to replace it with, in this case, right now, I'm gonna open up my brackets
and in capital letters I can say redacted
just as an example. Ok. So now all I need to do
is to simply print in brackets, secure underscore message.
And now if I run my program,
there you go, use a password reducted.
So you can use the replace function to simply replace
any particular part of your string
with something else. And this is how you will do it
to round up. Let me show you one more thing and this is finding substrings,
say, for example,
we have a variable called email and it's equal to, let's say admin at lab cyber.com.
I'm pretty sure of course, you must have encountered this so many times before where
maybe you're trying to create a new account on the website
and they will ask you to provide an email address
if you type in something bogus. For example,
the system will be able to detect that, hey,
this isn't a valid email address and that's because
it could check to see if the at symbol was included in your input.
So if the at symbol isn't there, then obviously it knows, OK,
whatever this person typed in cannot be an email address.
Let me show you how we can write a very simple program
to check to see if the at symbol is in the string.
So from here,
I have my email it calls admin at labs.com. Let's check to see if the ad symbol is in it.
Ok? I'm gonna say if, ok,
if I know we haven't talked about this particular if statement yet, don't worry,
we will cover it in a lot more detail in the next section.
But for now
just know that if is a statement
that Python uses to check if something is true.
So I'm gonna say if and our email and then
dot
find,
this is the function that we can use to check to see if something exists in our string.
So that's the find function.
And now we have to specify what exactly are we looking for in brackets?
And now encodes the
at symbol.
So I'm trying to check to see if the at symbol exists.
And one way how we can verify that indeed,
it does exist. It is in a string is by saying
it is not equal to minus one
and then we add our colon.
So this not equals to minus one is a way of saying something does exist.
OK? So
right here
we're saying if in the email,
you found the at
symbol and it's not equal to minus one. So this confirms that yes, indeed.
The at symbol is in our string. So now we can do something
we can say print and now in brackets, we can say
a valid email format.
So now if I run my program,
there you go valid email format because the ad symbol was in fact found
if I remove the ad symbol
and now I run the program again, you can see nothing is printed out because
the ad symbol is no longer there.
It is not a valid email for mat, I go back, I add the ad symbol again. Iron
and now you can see it's working again.
So to give you a quick recap, first of all, we want to look for the add symbol in
an email address provided to us. So we're gonna say if
and then we're looking specifically for the add symbol. So if the add symbol
is present and we check to see if it's present with the not equals to minus one. So
if the add symbol is in fact present
simpleprint, valid email format. So take note of the find function.
So that's it for the video. Thank you for watching. I will see you in the next class.