1 00:00:01,020 --> 00:00:02,070 Instructor: Welcome back. 2 00:00:02,070 --> 00:00:06,453 Let's talk about the topic of formatted strings. 3 00:00:07,770 --> 00:00:10,800 Up until now, we've just written simple strings 4 00:00:10,800 --> 00:00:15,000 but we want a program that's dynamic, that's static. 5 00:00:15,000 --> 00:00:19,170 Let's say we have a Amazon page 6 00:00:19,170 --> 00:00:21,090 and we're working on Amazon, actually. 7 00:00:21,090 --> 00:00:23,940 And when a user logs into their profile, 8 00:00:23,940 --> 00:00:25,770 well, we wanna display their name 9 00:00:25,770 --> 00:00:27,483 or what they have in the cart. 10 00:00:28,440 --> 00:00:32,189 In that case, we don't wanna just hard code and write 11 00:00:32,189 --> 00:00:37,080 every single user's name, like Andrei or another user Joe, 12 00:00:37,080 --> 00:00:39,930 and write every single name in the world. 13 00:00:39,930 --> 00:00:42,870 No, ideally what we can do is 14 00:00:42,870 --> 00:00:44,910 have something dynamic. 15 00:00:44,910 --> 00:00:47,280 Where let's say it's a profile page 16 00:00:47,280 --> 00:00:51,000 and we simply use the name variable 17 00:00:51,000 --> 00:00:53,610 and we display that on the page. 18 00:00:53,610 --> 00:00:55,410 Now, this name variable 19 00:00:55,410 --> 00:00:59,040 should equal whatever the user's name is. 20 00:00:59,040 --> 00:01:01,410 And this is something that we can grab from the database. 21 00:01:01,410 --> 00:01:03,870 Again, something that we'll talk later on in the course. 22 00:01:03,870 --> 00:01:06,870 But let's assume that in here when we do equals 23 00:01:06,870 --> 00:01:09,513 we're gonna grab some user information, 24 00:01:10,410 --> 00:01:12,270 which will be the name. 25 00:01:12,270 --> 00:01:16,110 And this name for now will be John. 26 00:01:16,110 --> 00:01:17,730 Or let's go Johnny. 27 00:01:17,730 --> 00:01:21,603 And this Johnny, we wanna display on the profile page. 28 00:01:22,440 --> 00:01:25,263 And we can do that with formatted strings. 29 00:01:26,670 --> 00:01:31,670 We can simply, do something like print and then say name. 30 00:01:33,510 --> 00:01:37,650 But we also wanna greet that person, right? 31 00:01:37,650 --> 00:01:42,630 So let's do hi, and then + name, 32 00:01:42,630 --> 00:01:44,520 and remember to add a space. 33 00:01:44,520 --> 00:01:48,273 If I click run, I get hi Johnny. 34 00:01:49,590 --> 00:01:52,530 But as we get more and more information, 35 00:01:52,530 --> 00:01:57,090 let's say we have age, and Johnny is 55. 36 00:01:57,090 --> 00:01:59,070 If I wanted to extend the sentence, 37 00:01:59,070 --> 00:02:04,070 and I wanna say "you are" and then add another +, 38 00:02:05,370 --> 00:02:10,176 make sure we add a space in here and then add year old. 39 00:02:12,690 --> 00:02:13,950 Run here. 40 00:02:13,950 --> 00:02:16,020 Hmm. All right, we get a type error. 41 00:02:16,020 --> 00:02:18,630 Must be string not int. 42 00:02:18,630 --> 00:02:19,463 Oh boy. 43 00:02:19,463 --> 00:02:21,870 All right, so we have to convert this into a string. 44 00:02:21,870 --> 00:02:25,050 So remember we can do that like this. 45 00:02:25,050 --> 00:02:26,523 And then if I click run, 46 00:02:27,420 --> 00:02:30,210 hi Johnny. You are 55 year old, 47 00:02:30,210 --> 00:02:31,263 or years old. 48 00:02:33,930 --> 00:02:37,350 All right, that was a little cumbersome 49 00:02:37,350 --> 00:02:39,870 but we're making our Amazon page dynamic. 50 00:02:39,870 --> 00:02:42,540 We can grab different information from the database 51 00:02:42,540 --> 00:02:47,540 and we'll have in a string something according to the user. 52 00:02:47,910 --> 00:02:49,890 But there's a better way of doing this. 53 00:02:49,890 --> 00:02:52,320 And with formatted strings, 54 00:02:52,320 --> 00:02:56,343 all we need to do is add an f at the beginning. 55 00:02:57,690 --> 00:03:00,360 And this f at the beginning is going to tell Python, 56 00:03:00,360 --> 00:03:03,600 Hey this is going to be a formatted string, 57 00:03:03,600 --> 00:03:08,370 and instead of doing all this + and name and all this stuff 58 00:03:08,370 --> 00:03:11,193 and doing the str to convert the type, 59 00:03:12,300 --> 00:03:14,670 we can simply do something like this. 60 00:03:14,670 --> 00:03:19,670 Let's remove this and simply do brackets. 61 00:03:20,220 --> 00:03:25,220 And say name, and then again remove all of this, 62 00:03:28,740 --> 00:03:30,273 and say age. 63 00:03:31,590 --> 00:03:36,150 So I'm going to say hi, name, you are age years old, 64 00:03:36,150 --> 00:03:40,473 and if I click run here you see that it still works. 65 00:03:41,370 --> 00:03:45,420 This is a new feature of Python 3. 66 00:03:45,420 --> 00:03:48,030 By adding f to the beginning, it's saying, 67 00:03:48,030 --> 00:03:50,760 Hey this is going to be a formatted string 68 00:03:50,760 --> 00:03:54,570 and I want you to just make these variables 69 00:03:54,570 --> 00:03:58,890 available as strings inside of, well, this string. 70 00:03:58,890 --> 00:04:00,693 How much cleaner is that? 71 00:04:02,490 --> 00:04:04,650 Now, although this is nice and clean 72 00:04:04,650 --> 00:04:08,400 and this is my preferred way of writing strings 73 00:04:08,400 --> 00:04:13,140 before Python 3, you didn't really have this. 74 00:04:13,140 --> 00:04:14,340 So in Python 2, 75 00:04:14,340 --> 00:04:16,680 and mind you this works in Python three as well. 76 00:04:16,680 --> 00:04:17,730 As you can see. 77 00:04:17,730 --> 00:04:20,283 We had something different to accomplish this. 78 00:04:21,120 --> 00:04:25,323 What we had was this idea of a .format. 79 00:04:26,640 --> 00:04:29,760 And .format, so let's remove the f here, 80 00:04:29,760 --> 00:04:32,340 is going to do the same thing for us. 81 00:04:32,340 --> 00:04:37,340 We do a bracket here and then we say, 82 00:04:37,830 --> 00:04:42,830 Hey, we want Johnny and age of 55, 83 00:04:43,650 --> 00:04:47,163 and we can just remove these variable names. 84 00:04:48,630 --> 00:04:53,630 If I click run here, hmm, I get an error. 85 00:04:53,910 --> 00:04:55,567 And you have to be careful here, 86 00:04:55,567 --> 00:04:58,950 .format works on strings, 87 00:04:58,950 --> 00:04:59,940 but you can see over here 88 00:04:59,940 --> 00:05:02,610 that we did it outside of the brackets. 89 00:05:02,610 --> 00:05:04,260 And what it's doing is saying, 90 00:05:04,260 --> 00:05:09,260 Hey, run the format action on this print, 91 00:05:10,770 --> 00:05:13,500 but print is not really a string, right? 92 00:05:13,500 --> 00:05:18,060 So you have to make sure that we move the brackets 93 00:05:18,060 --> 00:05:22,260 to the outside so that we evaluate this piece of code first. 94 00:05:22,260 --> 00:05:25,590 So the string is going to get formatted 95 00:05:25,590 --> 00:05:28,110 and then we're going to print. 96 00:05:28,110 --> 00:05:33,110 If I run this, hi Johnny you are 55 years old. 97 00:05:34,110 --> 00:05:36,690 So that works the same way. 98 00:05:36,690 --> 00:05:40,020 But what if we wanted to use variables? 99 00:05:40,020 --> 00:05:42,840 Because right now we're just doing this by order. 100 00:05:42,840 --> 00:05:45,990 So whatever comes first gets filled in first 101 00:05:45,990 --> 00:05:47,490 in the brackets. 102 00:05:47,490 --> 00:05:52,490 Well, we can do something like name and age. 103 00:05:53,610 --> 00:05:54,810 Let's see. Can we do that? 104 00:05:54,810 --> 00:05:55,643 Let's run. 105 00:05:56,580 --> 00:05:59,760 There you go. That works the same way. 106 00:05:59,760 --> 00:06:01,560 What if we had a specific order? 107 00:06:01,560 --> 00:06:06,560 Maybe we want this to be age and here to be name. 108 00:06:06,930 --> 00:06:09,000 Well, we can mix those around. 109 00:06:09,000 --> 00:06:13,290 We can say 1 here and 0 here, 110 00:06:13,290 --> 00:06:15,660 because in computer science 111 00:06:15,660 --> 00:06:17,850 we always start counting from 0. 112 00:06:17,850 --> 00:06:21,150 So this is 0 and this is 1. 113 00:06:21,150 --> 00:06:23,130 If we had something else in here 114 00:06:23,130 --> 00:06:27,260 let's say a third variable, then this will be 2. 115 00:06:29,430 --> 00:06:30,873 So if I click run here, 116 00:06:31,890 --> 00:06:34,173 all right, everything is upside down. 117 00:06:35,220 --> 00:06:38,580 Finally, I can just create my own variables if I wanted to. 118 00:06:38,580 --> 00:06:40,080 So let's say hi. 119 00:06:40,080 --> 00:06:44,863 Let's say new_name = sally 120 00:06:47,340 --> 00:06:52,267 and then age of sally is going to equal, let's say 100. 121 00:06:53,730 --> 00:06:55,260 Sally is very old. 122 00:06:55,260 --> 00:06:59,160 Now, if I do 0, let's say and 1 here. 123 00:06:59,160 --> 00:07:00,183 I click run. 124 00:07:01,260 --> 00:07:04,200 So now we'll actually get an error, 125 00:07:04,200 --> 00:07:05,790 tuple index out of range. 126 00:07:05,790 --> 00:07:08,850 Now, we haven't really learned about tuples and ranges 127 00:07:08,850 --> 00:07:12,540 and this is a little confusing, but a bit of a trick here. 128 00:07:12,540 --> 00:07:14,516 We want to make sure that we add, now, 129 00:07:14,516 --> 00:07:19,516 because we've given the actual variable a value. 130 00:07:21,750 --> 00:07:26,750 We need to actually say new_name here and here will be age. 131 00:07:30,120 --> 00:07:31,623 So that if I click run. 132 00:07:32,730 --> 00:07:35,493 There you go, hi sally you are 100 years old. 133 00:07:37,020 --> 00:07:39,870 As you can see with .format 134 00:07:39,870 --> 00:07:42,540 things are a little bit more complicated. 135 00:07:42,540 --> 00:07:46,680 You'll still see this in all Python 2 code 136 00:07:46,680 --> 00:07:48,870 still uses the .format. 137 00:07:48,870 --> 00:07:51,180 And you'll see a lot of Python 3 code bases 138 00:07:51,180 --> 00:07:55,110 that still use .format because some people prefer it. 139 00:07:55,110 --> 00:08:00,110 But I would argue that the f at the beginning 140 00:08:00,660 --> 00:08:03,810 of a formatted string is the way to go, 141 00:08:03,810 --> 00:08:07,650 because well it just makes things so much easier, right? 142 00:08:07,650 --> 00:08:10,590 Nice and clean, nice and easy. 143 00:08:10,590 --> 00:08:15,060 So I do recommend that you use the formatted string 144 00:08:15,060 --> 00:08:16,920 with the f in front. 145 00:08:16,920 --> 00:08:20,940 And for short, we usually call this an f string. 146 00:08:20,940 --> 00:08:22,966 All right, I'll see you in the next one. 147 00:08:22,966 --> 00:08:23,853 Bye bye.