1 00:00:00,450 --> 00:00:03,560 So let's start with a string variable. 2 00:00:03,560 --> 00:00:07,890 It is a quote from Albert Einstein and it's a great one for 3 00:00:07,890 --> 00:00:10,220 encouraging exploration here in the REPL. 4 00:00:10,220 --> 00:00:14,289 So he said a person who never made 5 00:00:14,289 --> 00:00:18,977 a mistake never tried anything new. 6 00:00:21,220 --> 00:00:24,847 So there's a built in function that lets you see the length of a string, 7 00:00:24,847 --> 00:00:26,870 how many characters it contains. 8 00:00:26,870 --> 00:00:28,270 It's name is len. 9 00:00:28,270 --> 00:00:32,800 So we'll play len, and then we'll pass in our object, our string object. 10 00:00:32,800 --> 00:00:34,340 We'll play len(quote). 11 00:00:34,340 --> 00:00:37,670 It has 58 characters, and that includes spaces. 12 00:00:37,670 --> 00:00:40,740 So len here, that's a function. 13 00:00:40,740 --> 00:00:43,900 Remember that everything you create is an object. 14 00:00:43,900 --> 00:00:47,800 So our string here that we created is an object, and 15 00:00:47,800 --> 00:00:51,710 all objects can have abilities or methods. 16 00:00:51,710 --> 00:00:56,800 You can think of methods as functions that belong to an object. 17 00:00:56,800 --> 00:01:01,570 And you can access methods that an object owns by using what is known as dot 18 00:01:01,570 --> 00:01:02,900 notation. 19 00:01:02,900 --> 00:01:07,590 For instance, there is a great method on strings called upper. 20 00:01:07,590 --> 00:01:09,355 It creates a new string, 21 00:01:09,355 --> 00:01:12,985 where all the characters in the original string are converted to uppercase. 22 00:01:12,985 --> 00:01:16,375 It's a good way to make the computer yell in all caps. 23 00:01:16,375 --> 00:01:19,895 And I can access the methods on a specific object, 24 00:01:19,895 --> 00:01:24,935 so lets use our quote object here by placing a period after the variable name. 25 00:01:24,935 --> 00:01:28,755 Now, this period here, this is why it's called dot notation. 26 00:01:28,755 --> 00:01:32,055 And now, I can access the object's methods. 27 00:01:32,055 --> 00:01:35,070 So that method was upper. 28 00:01:35,070 --> 00:01:36,550 And it's just like a function. 29 00:01:36,550 --> 00:01:39,210 So I call it using parenthesis. 30 00:01:39,210 --> 00:01:42,210 In this particular method, it doesn't take any extra arguments. 31 00:01:42,210 --> 00:01:45,369 So we can leave the inside of this empty. 32 00:01:45,369 --> 00:01:47,020 And there, 33 00:01:47,020 --> 00:01:51,988 you'll see we returned a new string of Einstein yelling motivation at you. 34 00:01:51,988 --> 00:01:54,970 And, again, note it didn't change the original string, 35 00:01:54,970 --> 00:01:58,510 and that's because you can't change a string. 36 00:01:58,510 --> 00:02:00,010 It returned a new string. 37 00:02:01,320 --> 00:02:02,970 So as you can imagine, 38 00:02:02,970 --> 00:02:06,480 there's also a method named lower that does a similar thing. 39 00:02:06,480 --> 00:02:12,090 And so, the string owns the method, lower, and we access it by placing a dot. 40 00:02:13,090 --> 00:02:17,190 And then, we do the method name, which was lower, and we call it. 41 00:02:17,190 --> 00:02:20,670 Which the only character here that's different is that leading a. 42 00:02:20,670 --> 00:02:23,830 So this is Einstein whispering his advice to you. 43 00:02:23,830 --> 00:02:28,160 Now, if you're heeding his advice, I'd recommend that you pause me and 44 00:02:28,160 --> 00:02:31,840 attempt to use a method on the string called title. 45 00:02:31,840 --> 00:02:35,760 What it does is it turns all words in the quote to title case, 46 00:02:35,760 --> 00:02:38,930 meaning each word has a capitalized first letter. 47 00:02:38,930 --> 00:02:40,010 Go ahead and try it. 48 00:02:40,010 --> 00:02:40,510 Pause me. 49 00:02:41,932 --> 00:02:43,320 Are you ready? 50 00:02:43,320 --> 00:02:44,628 Here I go. This is what I did. 51 00:02:44,628 --> 00:02:48,918 I did quote, and then I did a dot to access its methods, title, and 52 00:02:48,918 --> 00:02:50,079 then I called it. 53 00:02:52,079 --> 00:02:55,885 So that could be the title of your autobiography, am I right? 54 00:02:55,885 --> 00:03:01,160 You can also turn most objects into their string version using the built in str, 55 00:03:01,160 --> 00:03:03,030 which is short for string. 56 00:03:03,030 --> 00:03:06,460 Now, this works the same way that we coerce strings to ints, 57 00:03:06,460 --> 00:03:07,520 it's just the reverse, right? 58 00:03:07,520 --> 00:03:13,850 So we could say str(42), and we'll get back the string 42, not the integer 42. 59 00:03:13,850 --> 00:03:16,810 If you're curious to see what other methods a string owns, 60 00:03:16,810 --> 00:03:22,400 you can see them by doing help and an (str). 61 00:03:22,400 --> 00:03:27,120 So you'll note we have methods defined here, and they're in alphabetical order. 62 00:03:27,120 --> 00:03:30,660 Now, these ones that start with these double underscores, 63 00:03:30,660 --> 00:03:32,640 these are known as magic methods. 64 00:03:32,640 --> 00:03:36,220 So let's skip past those for now, we've got plenty of magic already. 65 00:03:37,290 --> 00:03:39,940 So, the first keep on pressing space bar it's jumping. 66 00:03:39,940 --> 00:03:40,520 So there we go. 67 00:03:40,520 --> 00:03:44,340 I think I saw the first one outside of there is capitalized. 68 00:03:44,340 --> 00:03:50,340 So here, this capitol S represents our string. 69 00:03:50,340 --> 00:03:52,830 And you can see it's calling capitalize with no 70 00:03:52,830 --> 00:03:54,880 arguments because it doesn't take any. 71 00:03:54,880 --> 00:03:57,200 And it returns a new string. 72 00:03:57,200 --> 00:04:01,230 That's what this arrow means, it means it will return you a new string. 73 00:04:01,230 --> 00:04:04,752 And the instructions of what it says is it will return a capitalized version of S and 74 00:04:04,752 --> 00:04:07,764 make the first character have upper-case and the rest lower-case, 75 00:04:07,764 --> 00:04:09,760 which our string already does. 76 00:04:09,760 --> 00:04:10,740 Now please, 77 00:04:10,740 --> 00:04:14,450 don't feel like you should be able to read all this documentation right now. 78 00:04:14,450 --> 00:04:18,190 We'll get there, but you should be starting to recognize some words and 79 00:04:18,190 --> 00:04:19,210 concepts. 80 00:04:19,210 --> 00:04:21,560 Just like foreign language immersion. 81 00:04:21,560 --> 00:04:23,160 Isn't that kind of cool? 82 00:04:23,160 --> 00:04:27,440 Now unfortunately, most documentation is not written with a beginner in mind, and 83 00:04:27,440 --> 00:04:30,020 some terms and knowledge are taken for granted. 84 00:04:30,020 --> 00:04:32,230 But hey, that's what we're here for, right? 85 00:04:32,230 --> 00:04:35,230 We'll get you reading this type of documentation in no time. 86 00:04:35,230 --> 00:04:37,120 Keep immersing yourselves. 87 00:04:37,120 --> 00:04:40,610 Okay, now press Q to drop out of the help documentation. 88 00:04:40,610 --> 00:04:44,090 And I'd like to show off a pretty handy feature of strings that you'll 89 00:04:44,090 --> 00:04:45,290 use quite a bit. 90 00:04:45,290 --> 00:04:49,580 It's called string formatting, and it allows you to create 91 00:04:49,580 --> 00:04:53,840 a reusable template that can be populated with different data. 92 00:04:53,840 --> 00:04:56,500 Think of these kind of like a mail merge. 93 00:04:56,500 --> 00:05:01,100 For instance, let's build one for an e-mail subject for treehouse students. 94 00:05:01,100 --> 00:05:04,700 The e-mail subject when populated would probably look something like this. 95 00:05:04,700 --> 00:05:08,044 We'll say thanks for learning JavaScript 96 00:05:10,132 --> 00:05:12,030 with us Craig. 97 00:05:12,030 --> 00:05:14,410 That's just one of the topics that we teach is JavaScript. 98 00:05:14,410 --> 00:05:18,410 So what we could do is we could define a template just like this for 99 00:05:18,410 --> 00:05:20,495 all topics and for all students. 100 00:05:20,495 --> 00:05:22,499 And all we would have to do is just switch out the variable. 101 00:05:23,560 --> 00:05:25,530 So what you do is you create a variable. 102 00:05:25,530 --> 00:05:31,230 We'll call it subject_template, and we'll make it so we can reuse this later. 103 00:05:31,230 --> 00:05:35,729 And now, this could be named anything, don't worry about subject_template. 104 00:05:35,729 --> 00:05:39,247 And what happens is you put your string that you want your template. 105 00:05:39,247 --> 00:05:41,890 So we say thanks for learning. 106 00:05:41,890 --> 00:05:44,550 And then, when you want to replace something, you add a placeholder. 107 00:05:44,550 --> 00:05:46,185 And those look like this. 108 00:05:46,185 --> 00:05:50,072 It's an open curly bracket and then a closed curly bracket. 109 00:05:50,072 --> 00:05:52,631 So Thanks for learning { } with us. 110 00:05:52,631 --> 00:05:55,650 And then, we're going to also change the name out. 111 00:05:55,650 --> 00:05:57,430 We're going to put another placeholder. 112 00:05:57,430 --> 00:06:02,734 So we'll do an { } and we'll do an !. 113 00:06:02,734 --> 00:06:03,438 Close that. 114 00:06:05,954 --> 00:06:08,600 So now that we have a template, we can fill it with data. 115 00:06:08,600 --> 00:06:12,620 So we're going to use the format method on strings. 116 00:06:12,620 --> 00:06:13,855 It is a method that they own. 117 00:06:13,855 --> 00:06:17,170 subject_template.format. 118 00:06:17,170 --> 00:06:21,266 And the way that this works is you put in the first parameter, we'll replace 119 00:06:21,266 --> 00:06:25,560 the first place holder and the second one will replace the second place holder. 120 00:06:25,560 --> 00:06:29,437 So the first parameter that we want to push in, we want to say thanks for 121 00:06:29,437 --> 00:06:30,900 learning Python with us. 122 00:06:32,070 --> 00:06:34,360 And so, Python is the topic. 123 00:06:34,360 --> 00:06:37,245 And Valentina is the name that we'll use here. 124 00:06:38,343 --> 00:06:41,471 Thanks for learning Python with us Valentina! 125 00:06:41,471 --> 00:06:42,710 Awesome, right? 126 00:06:42,710 --> 00:06:44,460 And you can keep using different strings, right? 127 00:06:44,460 --> 00:06:46,054 So here, we also teach Java. 128 00:06:47,666 --> 00:06:52,090 Somebody that I appreciate on our team here. 129 00:06:52,090 --> 00:06:54,886 We have Thanks for learning Java with us Shadd. 130 00:06:54,886 --> 00:06:57,520 Awesome, right? 131 00:06:57,520 --> 00:07:01,010 Now, numbers will automatically be coerced. 132 00:07:01,010 --> 00:07:02,770 For instance, if we had a purchasing screen, 133 00:07:02,770 --> 00:07:08,070 like something said you just bought, and then we put a placeholder for 134 00:07:08,070 --> 00:07:13,580 a number of items, and then we put a placeholder for what the item was. 135 00:07:13,580 --> 00:07:17,266 So we say you just bought, now we'll do .format, 136 00:07:17,266 --> 00:07:20,026 and we can pass in 3 and fidget cubes. 137 00:07:21,682 --> 00:07:26,420 And there, you see you just bought 3 fidget cubes. 138 00:07:26,420 --> 00:07:29,960 See how the 3 was automatically coerced to a string? 139 00:07:29,960 --> 00:07:31,510 But wait, there's more. 140 00:07:31,510 --> 00:07:35,140 One more powerful feature of strings is that you can check to see if 141 00:07:35,140 --> 00:07:37,470 one string is contained in another. 142 00:07:37,470 --> 00:07:39,820 Now, you can do this with the keyword in. 143 00:07:40,860 --> 00:07:43,380 It ends up reading very clear. 144 00:07:43,380 --> 00:07:50,920 So for instance, if you want to see is there ham in hamster, It returned true. 145 00:07:50,920 --> 00:07:54,540 This is saying that this expression is true with a capital T. 146 00:07:54,540 --> 00:07:58,580 It is true that the word ham is actually in hamster. 147 00:07:58,580 --> 00:08:00,100 And the reverse is also true. 148 00:08:00,100 --> 00:08:04,593 Is the word popcorn in hamster. 149 00:08:04,593 --> 00:08:07,820 It's false. 150 00:08:07,820 --> 00:08:08,920 And you can see that, right? 151 00:08:08,920 --> 00:08:11,540 It's returned us back a False with a capital F. 152 00:08:11,540 --> 00:08:12,780 Now, these true and 153 00:08:12,780 --> 00:08:16,200 false values are actually a different data type that we should explore. 154 00:08:16,200 --> 00:08:18,180 These are called Booleans. 155 00:08:18,180 --> 00:08:21,810 It's true that we're about to tie up this string exploration. 156 00:08:21,810 --> 00:08:25,860 So take a quick break and then come back refreshed ready to dive into Booleans.