Welcome back. Recall that types are like categories of
data and that different types are good for storing different kinds of things.
So for example, we've seen integers which are good for storing round numbers,
and we've seen floats which are good for storing floating points or decimals.
In this lesson, we're going to learn three new kinds of types,
strings, lists and tuples.
All three of these types are sequences.
A sequence is an ordered collection,
meaning that it's a collection of items and it has an order.
Meaning that it has the first item,
a second item, a third item and so on.
It also has a length.
So, let's start by talking about strings.
The simplest way to create a string is with the string literal expression.
So, for example here,
we're creating a string and storing its value in the variable
s. I create a String by starting with either double quotes,
or as I'll talk about in a little bit, single quotes.
Then inside of those quotation marks,
then I put the content of the string.
The content of the string is a sequence of characters or letters.
So, in the case of this string,
the first character is capital H,
the second character is lowercase e, and so on.
In this string, we have exactly one, two, three,
four, five, six, yes the space counts,
seven, eight, nine, 10,11 characters.
So, I would say that the string s is 11 characters long.
We can also create strings using single quotes.
So, if I replace these double quotes with single quotes,
then s is still a valid string.
If we wanted to create a multi-line string,
we can use triple quotes.
So, now if I print out the value of s and I get Hello world.
If I print out the value of m,
then I get this multi-line string.
For multi-line string, we can make these triple quotes either double quotes or
single quotes, either is valid.
So, we can use either double quotes or single quotes as
long as we're consistent about which kind of quotation we're using.
So, for example, I can't start out this string
s with double quotes and end it with a single quote.
If I tried to do that,
then I get a syntax error on line one.
Instead, I need to either use double quotes for both the start and end,
so I could do this, or I'd need to use single quotes for both the start and end.
The same is true for multi-lines strings.
We can either use three double quotes or three single quotes.
So, I'm going to modify this string s a little bit.
So, first, here's another valid string,
s equals capital M,
and so if I print it out then I get M. So,
what this is doing is, it's creating a string that's one character long,
and that first character is capital M. Here's another slightly more confusing string.
So here, s is a string that has zero characters in it.
So, s has a length of zero,
and that's not to be confused with a string that has one character,
but that character is a space.
Here the length of s is one,
whereas here if I delete this space,
the length of s is zero.
One more note for strings,
I can have a string that looks a lot like an integer.
So, for example, I can say s equals five,
and when I print out the value of s, then I get five.
To Python, this is entirely different than saying something like i equals the integer i.
So, even though when I print these two things out,
they look almost exactly the same in their representation,
there are things that I can do with s or things that I
can do with i that I can't do with the other,
because s is a string and i is an integer.
Just to show that these are different types,
I'm going to print out the type of s,
and I'm going to print out the type of i.
So, when I run this code,
I see that i and s have different types,
but this actually has consequences for what I can do with i and s. So, for example,
let's suppose that I wanted to print out what 10 plus i was,
I can say print out i plus 10.
Now, when I print out s then I get five,
and when I print out i plus 10,
then I get 15.
What if I tried to add 10 to s?
So, I printed out s plus 10.
Well, if I try to do this,
then Python is actually going to give me a run time error,
and it's going to say that it can't concatenate string and integer objects on line four.
Just to break that down, what it's saying is that s is
a string and I can't add an integer to a string,
because even though s is a string whose first character happens to be five,
to Python that's still a string.
But, even though to us,
s looks like a number, to Python,
s is just a sequence of characters,
and so we can't add a number to a sequence of characters.
To Python that doesn't make any sense to do,
and so we get a run time error.
If we did want to add five to something like s,
then we would need to cast it first.
So, I could say print int(s),
in this call to the function int is going to cast the string s to be an integer,
so the value of this overall expression is the integer five.
So, now when I run my code,
then I can see that I can add 10 to the integer value of s. Alternatively,
I could cast it to be a floating point number,
so I could say float s plus 10,
and then I would get 15.0,
because the value of this expression is the string s or five casted to a float,
and so the value of this overall expression is 5.0.
When we add 5.0 plus 10,
then the value of this overall expression is 15.0.
So, one thing to note when we cast strings like
s into numbers by calling something like float s,
is that when we call float or int to cast the string,
then the string needs to be a valid number.
What that means is that if I have something other than numbers in the string,
so for instance let's suppose that I say 500,
when I try to cast s into a float,
then I get a run time error,
and what this is saying is that the string 500 is not a number.
In other words, Python doesn't know how to convert this string into a float,
and the same goes for integers.