Welcome back, everyone, to the series of lectures on Python basics in this lecture, we're talking
about strings.
Strings in Python are used to hold text information and are indicated with the use of single or double
quotes.
We can also think of a string as a sequence of characters.
And this is actually a super important idea the python, because this means there's actually an order
to the string.
So an example string would be something like ABCD PFG.
So seven letters or seven characters.
But what's really interesting here is Python understands that a is the first character of the string
and pythons actually going to apply an index with each character.
And it actually starts the index at zero.
So, for example, the letter A is at index zero.
The Letter B is an index one and so on.
So you can actually use this indexing to grab particular elements or characters from that string.
We're also going to be able to take what are known as slices of this string using bracket notation so
we could grab multiple characters in a row if we wanted to grab ABCD, for instance, we could actually
use bracket notation for that and we'll show you how to do that later on in the lecture.
We should also note that strings are just Python objects or data structures and their methods, we can
actually call directly from the string to transform it.
For example, there's a method to uppercase every letter in this string, and we'll explore that as
well.
Now, the ideas of indexing, slicing and method calls are actually going to apply to many other objects
and Python will be discovering.
So I want you to keep that in mind as you learn them for strings.
Well, are going to learn some things that are very specific towards strings, but then indexing slicing
a method calls will be able to use that later on for other objects as well.
OK, let's get started exploring strings in Python.
I'm going to head over to our code editor.
All right, so here I am inside our code editor.
Let's go ahead and just quickly explain the use of single and double quotes within a Python string,
so we can say Python string with single quotes.
So I can say hello and single quotes like this, or I can also use double quotes to have that string
there.
Keep in mind that if I were to run this example python file right now, it's if I were to say Python
example that Python go ahead and run that file.
I don't see any output.
That's because to actually see the output using the command line, I would need to actually print one
of these strings.
So now I can save that, run it.
Now I see the output.
OK.
So single quotes or double quotes doesn't really matter.
The one part where it actually may matter is if you actually want to essentially encapsulate something
of a single quote or a double quote.
So, for example, let's imagine the sentence I don't want to go.
So this is a single quote, but it's just floating there.
It's actually part of the word.
What you can do with Python is you simply wrap it around the other type like this.
I don't want to go.
And let's actually cut that and put it inside our print function.
And then I can run this in Python says, OK, I understand that this single floating single quote here
is OK and it actually prints out.
I don't want to go.
So that's how you can deal with double quotes or single quotes that are inside something.
You can just keep wrapping it around another set of quotes to actually understand or have Python understand,
which is the quote and which is kind of the string quote.
So what else can we do with strings?
There's a bunch of different method calls you can use on a string to then actually change the string.
You can also concatenate strings for this sort of thing.
We're actually going to create a variable.
So remember, I can assign variables by saying something like my variable name is equal to.
And then let's go ahead and have it be a string such as.
Hello, and let's go ahead and explore just a couple of methods on this.
So I'm going to say Prince.
My variable.
And so when I read this, I should see.
Hello.
Now what are method calls on objects in Python?
Method calls is when you have a variable like my variable here, and it's a specific data type within
Python.
You can call Dot.
And then what's going to happen is there's a bunch of different methods you can actually call, which
are going to affect the object.
Later on, when we learn about object oriented programming, we're going to discover how to create our
own methods.
Now let me show you an example of one of these methods.
There's a ton of them, and we won't use all of them.
But I do want you to be aware of the fact that these methods exist on objects.
They're essentially just transformations or augmentations you can perform on an object.
So a really obvious one is something like upper.
Now, often I see a student mistake is they just call the actual method, but they don't execute the
method to execute the method.
You have to actually call open and close parentheses on that method and add a little bit of spacing
here.
Just so it's clear what I'm doing.
I'm calling my variable the method name, which sometimes you have to look up on the internet or a documentation
to understand what this method does.
And then you have open and close parentheses to actually execute or run that method.
So upper, as the name implies, will actually uppercase every single letter here.
So we're going to save that.
Run that and now notice hello, isn't all uppercase.
There's also things like capitalize, capitalize, this kind of similar just uppercase is the first
one, so we save that change.
Run that and then we see hello.
Always remember that to actually execute methods, you have to actually call the parentheses if you
just do this.
And forget to do those princes and you run this, it doesn't actually give you an error, it just says,
oh, there's a built in method that's going to capitalize a string object.
And it's this memory location on your computer.
Let me know when you want to execute it.
So Python actually is pretty helpful in its kind of report to you.
It just says, OK, that's a capitalization method call.
It's on this object that lives at this part in your computer and memory.
I should point out later on, we're going to learn about something called attributes, which are more
characteristics of the actual object rather than transformations on the object.
And those won't have open close princes on them.
But for right now, you can kind of ignore those.
This is all going to make a lot more sense when we actually create our own objects with object oriented
programming.
But I just want you to be aware of the fact that there's method calls you can call on a string.
The last one, I want to show you something called split, which is actually going to create what's
known as a list, which we're going to learn about next.
But let me just make a little string here.
I'm going to make a sentence that says something like, Hello, Mike, how is your dog?
OK?
So I can see I have a long string here and this is under my variable.
And then I'm going to call Dot Split Open close parentheses with that split.
This is by default, it's going to split this string into multiple components, depending on what you
pass in for, what you should split on.
So by default, it's going to split on blank spaces.
So if I run this?
It says, OK, here are all the single words inside that string, and it returns them as a list, so
there's bracket notation here.
This is actually a list in Python where it learn about it next.
So just keep in mind that Split returns a list and you can split on whatever you want.
So for example, if there was a comma here that said, Hello, Mike Comma, how is your dog?
I could say split on the string comma note how I'm passing the string here with a comma.
And this just tells Python, Go ahead.
And instead of default, splitting on the white or essentially blank components here, the little spaces
split only on commas.
So use commas as your splitter.
Now, if I save that change and I run this, I see.
Hello, Mike, split into the first one and then how is your dog?
The other thing I could do is maybe split on letters, so maybe I could split on the O's.
Kind of a weird choice here, but it is possible so I can run this.
And now I've split on the Oh, so every time there's an o, i go ahead and chop off the O and split
so I can see how those chopped off mike space h o was shut off.
Then W is y o shut off, etc. You kind of get the idea here.
This will be really useful later on when we have to quickly maybe grab all the words in a sentence or
something like that.
So often you're just using the default, which is blank just to grab all the words in a sentence.
OK, so we have the ability to run methods on our actual strings.
The other thing I want to point out is we have the ability to index particular items from a string.
So for example, let's imagine I wanted to grab h this very first h that is capitalized on the string.
There's something called index notation and bracket notation that we can perform on objects to grab
indexed items.
The way this works is after the variable name you have open and close square brackets and then you're
going to pass in the number of the index location of the letter you want.
Indexing starts at zero, so this is zero one two three four, etc. So if I say for my variable index
zero, that should return the very first letter in that string.
So I run that and I see now, h.
Let's go ahead and try to grab em this capital M.
Well, how many index locations is that over?
Remember?
A string is a sequence of characters, and you should also note that spaces count as a character.
So this is going to be zero one two three four five and then six.
So if I now pass in six here, what's there we go.
So five percent six for my index location.
It grabs M.
I should also note that indexing can technically go backwards as well.
For example, if I wanted to grab that very last g instead of having to count all the way forward,
I could starting at zero count backwards.
So if h0, that means if I start going backwards in the string, this will be negative one.
This g was going to be negative one.
So we're going to see negative one here, save that change.
Now I can see there is a dog.
So again, indexing can go forward for positive numbers or backwards for negative numbers.
Often we'll use negative one as a quick way to grab the last item in a sequence.
Now that's indexing for a single item.
There is also the ability to grab slices, as we mentioned.
So let's imagine I want to grab the entire slice of hello.
I want to grab that word using slicing.
What's the actual syntax for that?
Well, you say you're starting index points, colon and then the index you want to go up to, but not
include.
So it's inclusive of the starting point and it's exclusive of the end index point.
So that means I'm going to say starting at zero go zero one two three four and then I'd have to go up
to five because I'm grabbing everything up to, but not including that end index point.
So I pass and five there save that change.
Run that, and now I get.
Hello.
Let's imagine if I were to try to pass in for super common beginner mistake is they say, OK, let me
just cut the letters.
Zero one two three four.
But what happens is the way the Python interprets this as you want everything up to, but not including
four.
So if you run that, you'd only get back.
Oops.
I forget to save that change.
OK, so now if you run that, you only get back.
Hell.
OK.
So again, this happens because it goes up to but not including that last index point.
So this is slice notation.
It's your starting point colon and then your endpoint that it goes up to, but not including the endpoint.
The last thing I want to note here is the ability to actually use f string literals.
So if string literals the way they work is, they're actually going to be able to insert different variables
into a string.
This is not super useful for us right now, but later on, when we learn about functions, it's going
to be super useful to just insert strings.
So, for example, we could be creating a website that actually takes in names and passwords, so I
could say something like the first name.
It was a.
And the last name.
His Porsche here.
And then let's imagine I wanted to print out something like, Hello, first name, last name.
So I say hello, and then I have two options here.
I could manually type in Jose Porteus.
I could say something like, Hello, Jose Portillo.
But maybe this is part of a larger program where I want this to just work for anybody, not specifically
for Jose Portillo, because once this changes to something like Mimi Portillo, then this is hardcoded
in to be Jose.
So what I want to figure out is how can I just by changing what's up here?
Have the print statement reflect any changes in first name and last name?
So if we run this right now, I get back.
Hello, Jose Bautista.
But I want to actually use these variable calls, so I only need to change what's up here once in case
maybe I have something like prints.
Last name is 40A, so I have to keep hard coding stuff in which is not what I want it to be flexible
to these variable names.
The way we do that is with f string literals.
What you do is two steps one at the start of the string.
Outside the quotes, you just put an F. There this f is just to tell Python, Hey, keep a lookout for
variables that I'm going to put in curly braces inside the string.
And as I mentioned then, the second step is wherever you have something you want to insert, you put
in curly braces and then you're going to put in the actual variable name instead.
So I can say hello variable first name space, and let's replace portela here with curly braces variable.
Last thing, and then we're going to do the exact same thing here and to replace Tortilla here with
variable last name.
Save those changes.
And now after you save those changes and run this, you'll see the exact same results.
Hello, Joseph TRIA, last name is Tortilla, but now let's imagine up here, maybe in this form or
something, I change this to.
Mike Smith, save those changes.
Now this is going to automatically update with just those variable names, so it's only dependent on
the variable names, which you can then change up here.
So now when you say and run, you see.
Hello, Mike Smith, last name is Smith.
Very useful, especially for inserting things that you don't know ahead of time.
A lot of times you're not going to know ahead of time what the exact first name and last name is.
Instead, it's going to be like user defined on the web page, so it's going to be really easy to then
just insert it based off the variable names.
OK, so that's it.
The things we talked about here were just basics of strings.
Different methods.
You can call in strings indexing and slicing on strings and then print formatting on strings.
I'll see you at the next lecture.