Welcome to Exploring Computing.
Today's video is strings.
So in this video, we're going to take a closer
look at how strings work.
And I want to start off with this problem
that we ran into in the last video.
So you'll recall I was writing to a file,
and I had two write statements.
So I wrote Go Stanford and then I wrote Go Cardinal.
And instead of ending up with Go Stanford on one line and Go
Cardinal in the other line, when we actually open up the file,
we discovered that Go Stanford and Go Cardinal end up
on the same line.
So how am I going to fix this?
All right, so up on the top here,
we have what we ended up with.
And down at the bottom, we have what we want.
So if we were to represent these as strings,
this is what we would have here.
So up at the top, quote, "go StanfordGo Cardinal,"
that's what we ended up with.
And then the second one, "Go Stanford slash nGo Cardinal,"
what is this slash n?
All right, that slash n is what we
refer to as an escape sequence.
And in this particular case, the slash n
represents new line, which inserts a carriage return.
Now as we talked about in the very first lecture
of the quarter, there are different ways computers use
to represent the end of line, and it's different
depending on whether we're working with Unix machines
or working with Windows machines.
And we have a really old, old, old, Mac.
They represent it a different way.
And Python the Python interpreter is actually
going to know the proper representation
for your specific computer, and it will translate that slash
n into the exact sequence of ASCII characters
or Unicode characters that is necessary for the particular
computer you're working on.
But just so we have a general way of doing it
for any type of computer that Python
is designed to work with, we're just going
to represent it with a slash n.
This is called an escape sequence
because that back slash there indicates
to Python that the next character that it sees
is not to be treated normally.
I'm supposed to escape the normal sequence of treating
characters and do something special
with the next character.
So don't treat that n there after the slash as just
a regular n, that I've printed an n out on the output, which
is what you would normally do if you saw an n.
It says, no, this is something different.
There's a special table of escape sequences.
Look it up in the table of escape sequences
and do what it says there.
And it looks it up in the table and it says, oh,
that slash n is not a regular n.
That is actually a carriage return.
And so there's a couple of these.
So probably the most common you run into are the slash
n's and the slash t's.
Slash n represents new line or carriage return.
Slash t represents a tab.
And so here, I've got this string Stanford slash
n slash t slash tUniversity.
And so that actually represents Stanford
followed by a new line followed by 2 tabs.
And if we were to actually look at the output,
this is what we would see.
We would see Stanford on one line,
University on the next line, but not
University at the beginning of the line
because there are those two slash t's representing tabs--
so University tabbed over a bit.
Here's a couple more escape sequences you might run into.
One question is, how do you represent a backslash?
You can't just put a backslash there
because that starts the escape sequence.
And so if you really want to backslash-- not super common,
but if you really want a backslash,
you have to put two backslashes in a row.
The first backslash says that's an escape character.
Get out of the normal sequence.
And the next backslash says, oh, this particular sequence
is backslash backslash, which represents a single backslash.
All right, we also need one for representing quotes.
So if I wanted a quote, I would represent
that with a backslash quote.
Why would I want to do that?
Well, suppose I actually want a quote in the output.
So suppose I wanted the output, quote, "give them the axe,"
end quote, a traditional Stanford cheer.
If I were to just put this in a string,
this is what the string would look like.
And you can see by my little not sign
or cancel sign at the end, don't do this.
What's going to happen here is I start off
the string with a quote, and I end the string
with a quote on the far right after cheer.
And then I place my double quotes in the middle.
And so the way this is actually going
to be interpreted by Python is it's
going to see that first pair of double quotes,
the initial double quote fall by the double quote
for give them the axe.
And I'll say, oh, that's a little string right there.
And you may recall from the last lecture, we have a string,
and there's no characters in it.
That is completely legal, and that is what
we refer to as an empty string.
And then it will just see the quote
after the word axe followed by dash a traditional Stanford
chair followed by quote.
I say, OK, that's a string because really,
what it's doing is it's mashing up the pairs of double quotes
here.
And then I'll say, but you've got these words
here, give them the axe.
I don't know what that is.
Patrick, I think this is illegal.
I think you need to fix your Python code.
So this won't work.
This won't work because the double quotes are paired up,
and because they're paired up, I can't put actual double quotes
into the string that I'm trying to create.
So the way I get around this is by the slash quote.
So I say quote slash quote or backslash quote give them
the axe, backslash quote, a traditional Stanford cheer.
What's going to happen is it's going
to see that first double quote, and it's
going to see the slash double quote,
and it will see the slash first.
And it will say, OK, so the next character I see,
which is a double quote, don't just
treat that like a double quote.
So don't treat it as if, you know,
that matches with the original double quote.
No, that's a special character.
That should not be treated as a double quote character, which
would terminate the string with our empty string there.
That's actually, oh, I want a double quote
in the actual output, not as a delimiting character
delineating when the string starts
and when the string ends.
So if you happen to need a double quote into string,
that's how you would do it.
All right, if you want to start doing some fancier
things with strings, one of the things to be aware of
is that strings actually work pretty similar to lists.
So lists and strings both act as sequences,
and there are certain things that you can
do with sequences in Python.
And as we saw previously, in order
to access individual elements in a list,
we have the square bracket notation.
So this case, we have a list of team members.
And I say I want to print the second team member.
But you'll recall it actually starts off numbered at 0.
So Casey would be 0.
Hank would be 1.
Tammy would be 2.
And so this would actually print the word Tammy.
And here I've got a name, which is a string--
Stanford.
And I tell it to print name bracket 0.
And so what this is going to do is
I can access each of the individual characters
in the string.
So string-- so name 0 would be S.
Name 1 would be T. Name 2 would be A. name three
would be N and so on.
So in this case, if I say print name zero,
that actually prints the S. I should caution you, though.
In contrast to the elements of the list
where I can access the individual elements
in a list and not only access and get the information out
with the list--
I can actually replace elements in the list.
We can't actually do this with strings.
So if I were to try and replace that S with some other letter,
that actually is not going to work.
All right, we also saw previously
that we can get the length of a list of items.
And so here, I am taking length of the course list
number of classes.
And so this would, like--
I think in our example, we had three classes.
So this would print I'm taking three classes.
And we could do the same thing with strength.
So if I say length name where name is the string Stanford,
that would actually print the number eight
because there are eight characters in Stanford.
One thing that you may have noticed--
when I've been talking about taking advantage of some
of the features built into Python,
you may have noticed a difference
between different ways that we're accessing these.
So I've told you, hey, you can go ahead and get
the length of something.
You can go ahead and print something out.
You can go ahead and close the file.
You can go ahead and append a file.
And there is actually a difference
between how these were accessed.
Some of them I just had the name of the--
we'll refer to it as a function followed
by a pair of parentheses followed by whatever
information we needed.
So length followed by the variable
that's storing a sequence, originally a list of items,
now we could see that you can either pass in something
referred to a list of items, or you can pass in something
to refer to a string.
And it will go ahead and print out
the number of items in the string.
We've definitely been doing this with the print statement.
We can print-- say print and then
pass in a string or a number, and it
will go ahead and print it out.
And in fact, we saw that you can actually
ask it to print a bunch of things in the same line.
So those are what we refer to as function.
So with the functions, you just have the name
that I've told you to use-- so length or print
or whatever followed by the preferences.
In contrast, this other way of doing things
is something we refer to as a method.
With the method, we have an existing variable
that is referring to an item followed by a dot followed
by the name of this method I've told you to use followed
by a pair of parentheses.
So with closing a file, I have the name
of a variable, which is referring to a file,
followed by dot followed by close followed
by a pair of parentheses.
And we also saw this with append.
So with append, I had the name of a variable, which
was referring to a list, followed
by dot followed by append followed by the item
that I want to append to that list.
So I don't want to go too much into the technical details
here, but I did want to acknowledge that
that, yes, there are two things that look like they're
kind of doing the same thing, but they have different syntax
rules.
So the main thing for our purposes
is they have totally different ways of accessing them.
I wouldn't say totally different,
but they have different, distinct, clearly different
ways of accessing them.
So you do need to know whether you're working
with a function or a method.
And in terms of why they're different, in some cases,
you could do one or the other.
But what's really going on is there
are differences in how the functions versus the methods
are used for organizing programs.
When you're writing complex programs,
whether you use one or the other is
going to have an effect on how your overall programs are
organized.
And there are definitely differences in terms
of how they're implemented.
The functions are easier to write on your own.
In order to understand how the methods work,
it's actually fairly messy.
You need quite a bit of understanding
of the details of how Python works in order
to properly use the methods.
All right, so with that little discussion,
I've got a bunch of methods I want to talk about.
And we're going to need some of these to work with files.
So bear with me here.
The first couple we don't need to use for files,
but they're pretty straightforward.
So I want to start off with these.
OK, so the isdigit method returns true
if a string is composed only of digits.
So let's say I've been reading for files,
and I've been converting them to numbers by calling int or float
and converting them to numbers.
Maybe we've got a file that has a bunch of numbers mixed
in with a bunch of names, and I only
want to convert the ones that are actually numbers.
How would I know which one's a number and which one isn't?
Well, so one way you can do that is there's
an isdigit that works on strings,
and it returns either true or false.
You can see our little example here-- if data.isdigit.
So that's our method and application
that we saw a minute ago.
Name of a variable referring to a string followed by a followed
by the name of the method, which in this case
is isdigit, followed by a pair of parentheses--
whether or not that pair of parentheses
is empty or not depends upon the particular method we're using.
OK, so if data is digit, print is a number.
Else, contains letters or symbols.
So if data were a variable storing the string 1234,
data.isdigit would say, yes, this is entirely
consisting of digits.
So it would go ahead and print is a number.
OK, isalpha is sort of the opposite.
Isalpha says this is going to return true
if the string is only composed of letters of the alphabet.
So if data.isalpha, print all letters.
Else, print contains numbers or symbols.
So if data is set to quote "z24,"
this would print contains numbers or symbols
because there's the 24 in there.
All right, so here's the ones we really need.
So I mentioned this concept of white space before.
White space refers to any character that creates
blank space on a screen.
So that's pretty much the spaces.
If you have a whole bunch of spaces,
that's obviously going to generate a bunch of blank space
on in the document.
So if you have a bunch of tabs, that
generates a bunch of white space in the document,
blank space in the document.
Carriage return, if you have a bunch of carriage returns,
you're creating a bunch of blank lines.
And so that's going to create a lot of blank space
in the document.
So again, these characters that are creating
these blank spaces, we just sort of grouped them all together
and say, oh, those are characters that
are generating white space.
And so these are methods that remove white space.
lstrip removes white space on the left side of the string.
rstrip removes white space on the right side of the string,
and then strip removes white space
on either side of the string.
So let's take a look at how these will work.
OK, so let's say we have this string here stored
in the variable original where we've got a bunch of spaces
followed by Stanford followed by a slash n
followed by a slash t.
So that slash n, that's my new line character.
The slash t, that's the tab.
And so both the spaces and a slash
n on a slash t, that's all white space.
So if I create a new variable called data
and I say what I want to store in data is the original string
and then call the method strip on it,
that is going to remove white space from both sides.
So it'll remove the spaces at the beginning of the string.
It will remove the slash and the t at the end of the string.
And so when we're done with this, the variable data,
we'll store the string quote, "Stanford," end
quote without the spaces, without the slash n,
without the slash t.
Those will all go away.
Now if you want to--
I think it's less common to need to strip the white space
from just one of the sides.
But if you wanted to, if we were to do, for example, rstrip,
this strips off the white space on the right side
of string, which in this case is the slash n and the slash t.
But it leaves the white space on the left side of the string.
So in this particular case, data would
be set to quote space space Stanford
because the white space on the left side of the string
is left, and only the white space
on the right side of string is removed.
All right, so why do I want to do this?
Why do I care about this?
All right, so it turns out that when I'm reading strings
from a file, it's actually going to leave the new lines
at the end of each character.
I've alluded to this a little bit earlier.
So here, I'm reading a bunch of names for a file
and storing them in a list.
If my list contains the following names--
Patrick, Molly, Craig, Tammy, Chloe--
the actual list that I'm going to end up with
says, quote, "Patrick slash n," quote, "Molly slash n,"
slash Craig slash n, and so on.
So we've talked before about how that end of the line
is actually represented by an actual bit sequence.
Whether we're using ASCII or Unicode or whatever
our character encoding is, there is a bit sequence associated
with the end of the line.
And so what we're seeing here is that Python
is going ahead and just copying those over and saying,
hey, there's a character at the end of the line.
I'm just going to go ahead and put it in your--
when you read the line, I'm going
to go ahead and not only include the visible characters.
I'm also going to include that invisible end of line
character.
You may also notice that this list down at the bottom
that Python has given us is marking
its strings with single quotes instead of double quotes.
So it turns out that in Python, you can represent strings
either with single quotes, or you can
represent with double quotes.
I've been using double quotes because most programming
languages use double quote.
So if you were to continue on studying computer science
and you were used to having single quotes for your strings,
that would be a bad habit to get into.
However, while Python is perfectly happy
to accept double quotes when we're writing our code
or when you're interacting with the Python shell--
in fact, when it spits the strings back to you,
it always uses the single quotes.
I'm not quite sure why they did it, but anyway,
so you will see those single quoted strings when you're
working with the Python shell.
You can use single quote strings if you want,
but again, I think it's a bad habit
to get to because most languages require
you to use double quotes.
All right, so you know, probably, I
don't want all those slash n's at the end of each
of these names.
It just seems kind of annoying.
I don't know about you, but if I have a kid,
I'm not naming it with a slash n at the end of their name.
Maddy does not have a slash n at the name of her name.
OK, so how am I going to get rid of it?
Well, here's a new version of the code that's going to read--
it's going to read the names from our file.
And you can see what I've done here is I've just
added name dot rstrip.
So what this is going to do is this
is going to read each of the individual names.
So I've got a little for loop, name, and dataFile.
And so name is going to be set to each of the lines of text
from our dataFile here or our names file here.
And what we're going to do is we're
going to strip off any of the extra white space
on the right side before I start into my name list,
before I append it on the end of my name list.
So in this case, those slash n's which
occur at the end of each line, that's
just going to get stripped off by the rstrip.
And so you can see I end up with a list that I want,
which is the list of the names from the file without the slash
n's on the end.
Right, that's for now.
I'll talk to you all soon.