1 00:00:07,910 --> 00:00:12,000 Welcome back. Recall that types are like categories of 2 00:00:12,000 --> 00:00:15,975 data and that different types are good for storing different kinds of things. 3 00:00:15,975 --> 00:00:20,309 So for example, we've seen integers which are good for storing round numbers, 4 00:00:20,309 --> 00:00:24,525 and we've seen floats which are good for storing floating points or decimals. 5 00:00:24,525 --> 00:00:27,965 In this lesson, we're going to learn three new kinds of types, 6 00:00:27,965 --> 00:00:31,250 strings, lists and tuples. 7 00:00:31,250 --> 00:00:33,755 All three of these types are sequences. 8 00:00:33,755 --> 00:00:35,809 A sequence is an ordered collection, 9 00:00:35,809 --> 00:00:39,030 meaning that it's a collection of items and it has an order. 10 00:00:39,030 --> 00:00:40,575 Meaning that it has the first item, 11 00:00:40,575 --> 00:00:43,455 a second item, a third item and so on. 12 00:00:43,455 --> 00:00:45,850 It also has a length. 13 00:00:46,310 --> 00:00:49,740 So, let's start by talking about strings. 14 00:00:49,740 --> 00:00:54,420 The simplest way to create a string is with the string literal expression. 15 00:00:55,180 --> 00:00:57,755 So, for example here, 16 00:00:57,755 --> 00:01:01,730 we're creating a string and storing its value in the variable 17 00:01:01,730 --> 00:01:06,650 s. I create a String by starting with either double quotes, 18 00:01:06,650 --> 00:01:09,455 or as I'll talk about in a little bit, single quotes. 19 00:01:09,455 --> 00:01:12,035 Then inside of those quotation marks, 20 00:01:12,035 --> 00:01:14,530 then I put the content of the string. 21 00:01:14,530 --> 00:01:18,890 The content of the string is a sequence of characters or letters. 22 00:01:18,890 --> 00:01:20,840 So, in the case of this string, 23 00:01:20,840 --> 00:01:23,600 the first character is capital H, 24 00:01:23,600 --> 00:01:27,745 the second character is lowercase e, and so on. 25 00:01:27,745 --> 00:01:34,770 In this string, we have exactly one, two, three, 26 00:01:34,770 --> 00:01:37,695 four, five, six, yes the space counts, 27 00:01:37,695 --> 00:01:41,535 seven, eight, nine, 10,11 characters. 28 00:01:41,535 --> 00:01:46,420 So, I would say that the string s is 11 characters long. 29 00:01:47,140 --> 00:01:51,355 We can also create strings using single quotes. 30 00:01:51,355 --> 00:01:54,650 So, if I replace these double quotes with single quotes, 31 00:01:54,650 --> 00:01:57,140 then s is still a valid string. 32 00:01:57,140 --> 00:02:00,020 If we wanted to create a multi-line string, 33 00:02:00,020 --> 00:02:02,490 we can use triple quotes. 34 00:02:09,020 --> 00:02:16,250 So, now if I print out the value of s and I get Hello world. 35 00:02:16,250 --> 00:02:18,995 If I print out the value of m, 36 00:02:18,995 --> 00:02:21,880 then I get this multi-line string. 37 00:02:21,880 --> 00:02:27,440 For multi-line string, we can make these triple quotes either double quotes or 38 00:02:27,440 --> 00:02:34,745 single quotes, either is valid. 39 00:02:34,745 --> 00:02:38,450 So, we can use either double quotes or single quotes as 40 00:02:38,450 --> 00:02:42,230 long as we're consistent about which kind of quotation we're using. 41 00:02:42,230 --> 00:02:45,470 So, for example, I can't start out this string 42 00:02:45,470 --> 00:02:50,445 s with double quotes and end it with a single quote. 43 00:02:50,445 --> 00:02:51,700 If I tried to do that, 44 00:02:51,700 --> 00:02:54,005 then I get a syntax error on line one. 45 00:02:54,005 --> 00:02:58,070 Instead, I need to either use double quotes for both the start and end, 46 00:02:58,070 --> 00:03:04,130 so I could do this, or I'd need to use single quotes for both the start and end. 47 00:03:04,130 --> 00:03:06,680 The same is true for multi-lines strings. 48 00:03:06,680 --> 00:03:11,375 We can either use three double quotes or three single quotes. 49 00:03:11,375 --> 00:03:15,800 So, I'm going to modify this string s a little bit. 50 00:03:15,800 --> 00:03:18,725 So, first, here's another valid string, 51 00:03:18,725 --> 00:03:21,275 s equals capital M, 52 00:03:21,275 --> 00:03:25,725 and so if I print it out then I get M. So, 53 00:03:25,725 --> 00:03:29,705 what this is doing is, it's creating a string that's one character long, 54 00:03:29,705 --> 00:03:38,020 and that first character is capital M. Here's another slightly more confusing string. 55 00:03:38,020 --> 00:03:44,400 So here, s is a string that has zero characters in it. 56 00:03:44,400 --> 00:03:46,750 So, s has a length of zero, 57 00:03:46,750 --> 00:03:50,810 and that's not to be confused with a string that has one character, 58 00:03:50,810 --> 00:03:52,190 but that character is a space. 59 00:03:52,190 --> 00:03:54,215 Here the length of s is one, 60 00:03:54,215 --> 00:03:56,900 whereas here if I delete this space, 61 00:03:56,900 --> 00:03:59,110 the length of s is zero. 62 00:03:59,110 --> 00:04:01,270 One more note for strings, 63 00:04:01,270 --> 00:04:04,490 I can have a string that looks a lot like an integer. 64 00:04:04,490 --> 00:04:07,445 So, for example, I can say s equals five, 65 00:04:07,445 --> 00:04:10,855 and when I print out the value of s, then I get five. 66 00:04:10,855 --> 00:04:18,380 To Python, this is entirely different than saying something like i equals the integer i. 67 00:04:18,380 --> 00:04:21,350 So, even though when I print these two things out, 68 00:04:21,350 --> 00:04:24,890 they look almost exactly the same in their representation, 69 00:04:24,890 --> 00:04:27,200 there are things that I can do with s or things that I 70 00:04:27,200 --> 00:04:29,360 can do with i that I can't do with the other, 71 00:04:29,360 --> 00:04:32,675 because s is a string and i is an integer. 72 00:04:32,675 --> 00:04:35,030 Just to show that these are different types, 73 00:04:35,030 --> 00:04:38,390 I'm going to print out the type of s, 74 00:04:38,390 --> 00:04:42,695 and I'm going to print out the type of i. 75 00:04:42,695 --> 00:04:45,905 So, when I run this code, 76 00:04:45,905 --> 00:04:49,130 I see that i and s have different types, 77 00:04:49,130 --> 00:04:54,800 but this actually has consequences for what I can do with i and s. So, for example, 78 00:04:54,800 --> 00:04:59,450 let's suppose that I wanted to print out what 10 plus i was, 79 00:04:59,450 --> 00:05:03,225 I can say print out i plus 10. 80 00:05:03,225 --> 00:05:07,000 Now, when I print out s then I get five, 81 00:05:07,000 --> 00:05:09,485 and when I print out i plus 10, 82 00:05:09,485 --> 00:05:11,525 then I get 15. 83 00:05:11,525 --> 00:05:14,605 What if I tried to add 10 to s? 84 00:05:14,605 --> 00:05:17,460 So, I printed out s plus 10. 85 00:05:17,460 --> 00:05:19,350 Well, if I try to do this, 86 00:05:19,350 --> 00:05:22,100 then Python is actually going to give me a run time error, 87 00:05:22,100 --> 00:05:27,595 and it's going to say that it can't concatenate string and integer objects on line four. 88 00:05:27,595 --> 00:05:30,630 Just to break that down, what it's saying is that s is 89 00:05:30,630 --> 00:05:35,480 a string and I can't add an integer to a string, 90 00:05:35,480 --> 00:05:40,675 because even though s is a string whose first character happens to be five, 91 00:05:40,675 --> 00:05:42,690 to Python that's still a string. 92 00:05:42,690 --> 00:05:44,400 But, even though to us, 93 00:05:44,400 --> 00:05:46,975 s looks like a number, to Python, 94 00:05:46,975 --> 00:05:49,160 s is just a sequence of characters, 95 00:05:49,160 --> 00:05:52,480 and so we can't add a number to a sequence of characters. 96 00:05:52,480 --> 00:05:54,785 To Python that doesn't make any sense to do, 97 00:05:54,785 --> 00:05:57,170 and so we get a run time error. 98 00:05:57,170 --> 00:06:01,475 If we did want to add five to something like s, 99 00:06:01,475 --> 00:06:03,770 then we would need to cast it first. 100 00:06:03,770 --> 00:06:09,180 So, I could say print int(s), 101 00:06:09,180 --> 00:06:17,530 in this call to the function int is going to cast the string s to be an integer, 102 00:06:17,530 --> 00:06:23,260 so the value of this overall expression is the integer five. 103 00:06:26,240 --> 00:06:29,010 So, now when I run my code, 104 00:06:29,010 --> 00:06:35,375 then I can see that I can add 10 to the integer value of s. Alternatively, 105 00:06:35,375 --> 00:06:37,655 I could cast it to be a floating point number, 106 00:06:37,655 --> 00:06:41,060 so I could say float s plus 10, 107 00:06:41,060 --> 00:06:43,955 and then I would get 15.0, 108 00:06:43,955 --> 00:06:51,080 because the value of this expression is the string s or five casted to a float, 109 00:06:51,080 --> 00:06:55,385 and so the value of this overall expression is 5.0. 110 00:06:55,385 --> 00:06:59,105 When we add 5.0 plus 10, 111 00:06:59,105 --> 00:07:03,745 then the value of this overall expression is 15.0. 112 00:07:03,745 --> 00:07:07,265 So, one thing to note when we cast strings like 113 00:07:07,265 --> 00:07:11,795 s into numbers by calling something like float s, 114 00:07:11,795 --> 00:07:16,370 is that when we call float or int to cast the string, 115 00:07:16,370 --> 00:07:18,575 then the string needs to be a valid number. 116 00:07:18,575 --> 00:07:22,550 What that means is that if I have something other than numbers in the string, 117 00:07:22,550 --> 00:07:26,340 so for instance let's suppose that I say 500, 118 00:07:27,370 --> 00:07:33,055 when I try to cast s into a float, 119 00:07:33,055 --> 00:07:34,795 then I get a run time error, 120 00:07:34,795 --> 00:07:39,455 and what this is saying is that the string 500 is not a number. 121 00:07:39,455 --> 00:07:44,705 In other words, Python doesn't know how to convert this string into a float, 122 00:07:44,705 --> 00:07:47,550 and the same goes for integers.