-
[MUSIC PLAYING]
-
-
MOSH HAMEDANI: So here we
have this course variable
-
set to Python programming.
-
As I told you before,
whenever you work with text,
-
you should surround
your text with quotes.
-
You can either use double
quotes or single quotes.
-
That's more of a
personal preference,
-
but quite often we
use double quotes.
-
We also have triple
quotes, and we use
-
them to format a long string.
-
For example, if you have, let's
say, a variable message, that
-
is the message we want to
include in the body of an email.
-
You can use triple quotes
to format it like this--
-
Hi, John.
-
This is Mosh from
codewithmosh.com--
-
blah, blah, blah.
-
So that's when we
use triple quotes.
-
Now we don't need
in this lecture.
-
So delete.
-
Let me show you a few useful
things you can do with strings.
-
First of all, we have this
built-in function in Python
-
for getting the
lengths of strings.
-
What is a function?
-
A function is basically
a reusable piece of code
-
that carries out a task.
-
As a metaphor, think of the
remote control of your TV.
-
On this remote control, you have
buttons for different functions,
-
like turn on, turn off,
change the channel, and so on.
-
These are the built-in
functions in your TV.
-
In Python and many other
programming languages,
-
we have the exact same concept.
-
So we have functions
that are built
-
into the language
and the platform.
-
You can reuse these functions
to perform various tasks.
-
So here we can use the
built-in len function
-
to get the length
of a string, which
-
means the number of
characters in that string.
-
Now whenever you want
to use a function,
-
you should use parentheses.
-
Now we say we're calling this
function, which basically means
-
we are using this function.
-
Now some functions
take additional data,
-
which we refer to as arguments.
-
These arguments are
inputs to these functions.
-
So this len function takes
an input or an argument.
-
Here we pass our
course variable.
-
And this will return the number
of characters in this string.
-
So let's print that
and see what we get.
-
Run the program.
-
We get 18 because we
have 18 characters here.
-
Let's look at another example.
-
If you want to get access
to a specific character
-
in this string, you use the
square bracket notation.
-
So here we add course
square brackets.
-
To get the first character,
you use the index 0.
-
So in Python, like
many other languages,
-
strings are 0
indexed, which means
-
the index of the first character
or the first element is 0.
-
So now when we print
this, we'll get p.
-
Now you can also use a
negative index like minus 1.
-
What does that mean?
-
Well, if 0 represents
the first character here,
-
what do you think
negative 1 represents?
-
That takes us back to
the end of the string.
-
So that returns
the first character
-
from the end of the string.
-
Let's run this program.
-
You will see we'll get g.
-
There you go.
-
Using a similar syntax,
you can slice strings.
-
Let me show you.
-
So I'm going to duplicate this
line and remove negative 1.
-
Now let's say we want to
extract the first three
-
characters in this string.
-
So here we need two indexes--
-
the start index
colon the end index.
-
So this will return
a new string that
-
contains the first three
characters in this course
-
variable.
-
That will be p, y, and t.
-
So the index of these
characters are 0, 1, and 2.
-
So that means the character at
the end index is not included.
-
Let's run the program and make
sure we get the right result.
-
There you go--
-
Pyt.
-
Now, what if we don't
include the end index?
-
What do you think
we're going to get?
-
It's common sense.
-
We start from index
0 and go all the way
-
to the end of the string.
-
So this will return
a new string that
-
is exactly the same as
the original string.
-
Let's take a look.
-
So we get Python programming.
-
Now, what if we don't
include the start index
-
but include the end index.
-
What do you think
we're going to get?
-
Once again, it's common sense.
-
So by default, Python
will put 0 here.
-
So it will start from the
beginning of the string.
-
So when I run this program, we
should get Pyt one more time.
-
There you go.
-
And finally, as
the last example,
-
if we don't include the
start and the end index,
-
this will return a copy
of the original string.
-
Let's look at this.
-
So we get Python programming.
-
Now you don't have to
memorize any of these.
-
Just remember we
use the len function
-
to get the length of a string.
-
We use bracket
notation to get access
-
to a specific element
or a specific character.
-
And we use this notation
to slice a string.
-
-
[MUSIC PLAYING]
-
-
So we have this string
here, Python programming.
-
Now let's say we want
to put a double quote
-
in the middle of this string.
-
There is a problem here.
-
Python interpreter
sees this second string
-
as the end of the string.
-
So the rest of the code is
meaningless and invalid.
-
How do we solve this problem?
-
Well, there are two ways.
-
One way is to use single
quotes for our string,
-
and then we can
use a double quote
-
in the middle of the string.
-
But what if, for
whatever reason,
-
perhaps for being
consistent in our code,
-
we decided to use double quotes?
-
How can we add
another double quote
-
in the middle of this string?
-
Well, we can prefix
this with a backslash.
-
Backslash in Python strings
is a special character.
-
We have a jargon for that
called escape character.
-
We use it to escape
the character after.
-
Let me show you what I mean.
-
So let's print this course
and run this program.
-
What's going on here?
-
We don't have the
backslash because we
-
use that to escape
this double code
-
and basically display it here.
-
So backslash is an
escape character.
-
And backslash double quote
is an escape sequence.
-
In Python strings, we have
a few other escape sequences
-
that you should be aware of.
-
Let me show you.
-
So in Python, we use a hash
sign to indicate a comment.
-
A comment is like additional
note that we add to our program.
-
It's not executed by
Python interpreter.
-
So here are the
escape sequences.
-
You have seen
backslash double quote.
-
We also have backslash
single quote.
-
So we can use that to
add a single quote here.
-
Let's run the program.
-
Here it is.
-
Beautiful!
-
We also have double backslash.
-
So if you want to include a
backslash in your strings,
-
you should prefix it
with another backslash.
-
Let me show you.
-
So when we run this,
we get Python one
-
backslash programming.
-
And finally, we have backslash
n, which is short for new line.
-
So now if I add a backslash
n here, see what we get.
-
We get a new line after Python.
-
So programming will end
up on the second line.
-
So these are the escape
sequences in Python.
-
-
[MUSIC PLAYING]
-
-
Here we have two
variables-- first and last.
-
Let's say we want to print
my full name on the console.
-
So we can define another
variable, full, set it to first.
-
Then concatenate
it with a space.
-
And one more time
concatenate it with last.
-
Now when we print full, we get
my full name on the console.
-
Beautiful!
-
Now this approach of using
concatenation to build a string
-
is OK, but there is a
better and newer approach.
-
We can use formatted strings.
-
So here we can set
full to this string
-
and prefix it with an f, which
can be lowercase or uppercase.
-
This formatted string
doesn't have a constant value
-
like these two strings here.
-
It's actually an expression that
will be evaluated at runtime.
-
So here we want to
add our first name.
-
We use curly braces to print
the value of the first variable.
-
After that, we add
a space, and then we
-
add curly braces one more
time to print the last name.
-
So at runtime, this
expression will be evaluated.
-
What we have in
between curly braces
-
will be replaced at runtime.
-
Now let's run this
program one more time.
-
We get the exact same
result. Just be aware
-
that you can put any
valid expressions
-
in between curly braces.
-
So earlier, you learned about
the built-in len function.
-
We can call len here to get
the length of this string.
-
Let's run this program
one more time so we get 4.
-
We can also replace last with an
expression like this, 2 plus 2.
-
Let's run this program.
-
We get 4 and 4.
-
So when using
formatted strings, you
-
can put any valid expressions
in between curly braces.
-
-
[MUSIC PLAYING]
-
-
In this lecture, we're going to
look at a few useful functions
-
available to work with strings.
-
So earlier, you learned about
this built-in len function.
-
This function is
general purpose.
-
So it's not limited to strings.
-
Later I will show you
how to use this function
-
with other objects.
-
But in Python, we have
quite a few functions
-
that are specific to a strings.
-
Let me show you.
-
So here, if we type
course., see, all these
-
are functions
available on strings.
-
Now, in precise terms, we refer
to these functions as methods.
-
This is a term in
object-oriented programming
-
that you will learn about
later in the course.
-
For now, what I want
you to take away
-
is that everything in
Python is an object,
-
and objects have
functions we call methods
-
that we can access
using the dot notation.
-
So here course is an object.
-
We use the dot notation to
access its functions or more
-
accurately methods.
-
Let's take a look at a
few of these methods.
-
We have upper to convert
a string to uppercase.
-
Now, let's print this
and run the program.
-
Here is what we get.
-
Beautiful!
-
Now note that the methods
that you call here
-
return a new string.
-
So the original string
is not affected.
-
Let me show you.
-
So print course.
-
Run the program one more time.
-
Look.
-
This is our original string.
-
So course.upper returns a
new string, a new value.
-
We can store it in a variable
like course_capital like this.
-
Now to keep this demo
simple and consistent,
-
I'm going to revert this back
and use a print statement.
-
We also have the lower method to
convert a string to lowercase.
-
We also have title,
which will capitalize
-
the first letter of every word.
-
So if our string was like this,
when we call the title method,
-
we get Python programming
as you see here.
-
Another useful method
is strip, and we
-
use it to trim any
whitespace at the beginning
-
or end of a string.
-
This is particularly useful when
we receive input from the user.
-
Let me show you.
-
So let's imagine the user
entered a couple of white spaces
-
at the beginning of this string.
-
When we call course.strip, those
white spaces will be removed.
-
Take a look.
-
So note that in the
first three examples,
-
we have those white spaces.
-
But in the last
one, it is removed.
-
So a strip removes the white
space from both the beginning
-
and end of a string.
-
We also have lstrip, which
is short for Left Strip,
-
and rstrip, which is
short for Right Strip.
-
So it will remove the white
space from the end of a string.
-
If you want to get the
index of a character
-
or a sequence of
characters in your string,
-
you should use the find method.
-
Let me show you.
-
So course.find.
-
So as an argument, here
we pass another string.
-
We can pass a character
or a series of characters.
-
Let's find the index of pro.
-
Run the program.
-
So the index of pro is 9.
-
So if you start from 0
here all the way to 9,
-
this is the index of pro.
-
Now, as I told
you before, Python
-
is a case-sensitive language.
-
So if I pass a capital
P here, obviously we
-
don't have these exact
characters in our string.
-
So let's see what we get.
-
We get negative 1.
-
That means this string was not
found in the original string.
-
Another useful
method is replace.
-
So we call replace.
-
With this, we can
replace a character
-
or a sequence of characters
with something else.
-
So let's say we want to replace
all lowercase p's with j.
-
With this, we get
jython jrogramming,
-
whatever that means.
-
And finally, if
you want to check
-
for the existence of a character
or a sequence of characters
-
in your string, you can
use the in operator.
-
Let me show you.
-
So print, rewrite an expression
like this-- pro in course.
-
So this is an expression.
-
As I told you
before, an expression
-
is a piece of code
that produces a value.
-
So this expression checks to
see if we have pro in course.
-
The difference between this
expression and calling the
-
find method is that
the find method returns
-
the index of these
characters in our string,
-
whereas this expression
returns a Boolean,
-
so it's a true or false.
-
Let me show you.
-
So run the program.
-
We get the Boolean true.
-
And finally, we have
the not operator.
-
And we use that to see
if our string does not
-
contain a character or a
sequence of characters.
-
So let's change this
to swift not in course.
-
When this expression
is evaluated,
-
what do you think
we're going to get?
-
Well, we don't have
swift in this string,
-
so not in will return true.
-
Let's take a look.
-
There you go.
-
So these are the
useful string methods.
-
Next, we'll look at numbers.
-
-
Hi, guys.
-
Thank you for watching
this tutorial.
-
My name is Mosh
Hamedani, and I have
-
tons of tutorials like
this for you on my channel.
-
So be sure to subscribe!
-
And also please like
and share this video.
-
If you want to learn
Python properly
-
from scratch with depth, I have
a comprehensive Python tutorial
-
for you.
-
The link is below this video.
-
So click the link
to get started.
-
Thank you, and have
a fantastic day!
-