1 00:00:01,020 --> 00:00:01,590 Welcome back. 2 00:00:02,070 --> 00:00:06,330 Let's talk about the topic of formatted strings. 3 00:00:07,730 --> 00:00:14,690 Up until now, we've just written simple strings, but we want a program that's dynamic, that's static, 4 00:00:15,020 --> 00:00:20,780 let's say we have a Amazon page and we're working on Amazon, actually. 5 00:00:21,050 --> 00:00:26,960 And when a user logs into their profile while we want to display their name or what they have in the 6 00:00:26,960 --> 00:00:27,380 cart. 7 00:00:28,420 --> 00:00:35,950 In that case, we don't want to just hard code and write every single user's name like Andre or another 8 00:00:35,950 --> 00:00:40,270 user Jo, and write every single name in the world, no. 9 00:00:40,270 --> 00:00:48,520 Ideally, what we can do is have something dynamic where let's say it's a profile page and we simply 10 00:00:48,910 --> 00:00:52,630 use the name variable and we display that on the page. 11 00:00:53,620 --> 00:00:58,870 Now, this name variable should equal whatever the user's name is. 12 00:00:59,030 --> 00:01:03,070 And this is something that we can grab from the database again, something that we'll talk later on 13 00:01:03,070 --> 00:01:03,610 in the course. 14 00:01:03,850 --> 00:01:09,400 But let's assume that in here when we do equals we're going to grab some user information. 15 00:01:10,390 --> 00:01:15,670 Which will be the name and this name for now will be John. 16 00:01:16,120 --> 00:01:21,430 All right, let's go, Johnny and this Johnny we want to display on the profile page. 17 00:01:22,370 --> 00:01:25,100 And we can do that with formatted strings. 18 00:01:26,660 --> 00:01:32,360 We can simply do something like print and then say name. 19 00:01:33,500 --> 00:01:37,510 But we also want to greet that person, right? 20 00:01:37,640 --> 00:01:39,350 So let's do hi. 21 00:01:40,460 --> 00:01:48,140 And then plus name, I remember to outerspace, if I click run, I get Hi Johnny. 22 00:01:49,580 --> 00:01:58,400 But as we get more and more information, let's say we have age and Ajani is 55, if I wanted to extend 23 00:01:58,400 --> 00:02:07,250 the sentence and I want to say you are and then add another plus, make sure we add a space in here 24 00:02:08,090 --> 00:02:10,520 and then add your fault. 25 00:02:12,700 --> 00:02:14,250 Ron here, hmm. 26 00:02:14,560 --> 00:02:18,510 All right, we get to type air must be stirring, not into. 27 00:02:18,550 --> 00:02:19,100 Oh, boy. 28 00:02:19,240 --> 00:02:19,510 All right. 29 00:02:19,510 --> 00:02:21,910 So we have to convert this into a string. 30 00:02:21,920 --> 00:02:24,400 So remember, we can do that like this. 31 00:02:25,030 --> 00:02:26,350 And then if I click, run. 32 00:02:27,420 --> 00:02:31,140 Hi, Johnny, you are 55 year old or years old. 33 00:02:33,900 --> 00:02:40,860 All right, that was a little cumbersome, but we're making our Amazon page dynamic, we can grab different 34 00:02:40,860 --> 00:02:47,460 information from the database and we'll have in a string something according to the user. 35 00:02:47,910 --> 00:02:49,410 But there's a better way of doing this. 36 00:02:49,860 --> 00:02:56,250 And with formatted strings, all we need to do is add an F at the beginning. 37 00:02:57,650 --> 00:03:02,840 And this F at the beginning is going to tell Python, hey, this is going to be a formatted string, 38 00:03:03,590 --> 00:03:10,970 and instead of doing all this plus and name and all this stuff and doing the start to convert the type. 39 00:03:12,290 --> 00:03:21,440 We can simply do something like this, let's remove this and simply do Brackett's and say name. 40 00:03:22,660 --> 00:03:25,060 And then again, remove all of this. 41 00:03:28,740 --> 00:03:37,320 And say age, so I'm going to say hi name, you are eight years old, and if I click run here. 42 00:03:38,560 --> 00:03:40,300 You see that it still works. 43 00:03:41,350 --> 00:03:47,380 This is a new feature of Python three by adding F to the beginning. 44 00:03:47,630 --> 00:03:54,310 It's saying, hey, this is going to be a formatted string and I want you to just make these variables 45 00:03:54,520 --> 00:03:55,270 available. 46 00:03:55,460 --> 00:03:58,480 Strings inside of, well, this string. 47 00:03:58,900 --> 00:04:00,610 How much cleaner is that? 48 00:04:02,460 --> 00:04:09,390 Now, although this is nice and clean and this is my preferred way of writing strings before Python 49 00:04:09,390 --> 00:04:12,360 three, you didn't really have this. 50 00:04:13,140 --> 00:04:16,650 So in Python two and mind you, this works in Python three as well. 51 00:04:16,650 --> 00:04:20,100 As you can see, we had something different to accomplish this. 52 00:04:21,090 --> 00:04:25,230 What we had was this idea of a DOT format. 53 00:04:26,630 --> 00:04:31,370 And DOT format, so let's remove the F here, is going to do the same thing for us. 54 00:04:32,380 --> 00:04:40,090 We do a bracket here and then we say, hey, we want Johnny. 55 00:04:41,110 --> 00:04:47,020 And age of 55, and we can just remove these variable names. 56 00:04:48,630 --> 00:04:49,740 If I click run here. 57 00:04:51,200 --> 00:04:59,510 Hmm, I get an error and you have to be careful here, Daudt format works on strings, but you can see 58 00:04:59,510 --> 00:05:02,120 over here that we did it outside of the brackets. 59 00:05:02,600 --> 00:05:09,950 And what it's doing is saying, hey, Ron, the format action on this print. 60 00:05:10,740 --> 00:05:18,390 But print is not really a string, right, so you have to make sure that we move the brackets to the 61 00:05:18,390 --> 00:05:22,220 outside so that we evaluate this piece of code first. 62 00:05:22,260 --> 00:05:27,300 So the string is going to get formatted and then we're going to print. 63 00:05:30,670 --> 00:05:31,240 Hi, Johnny. 64 00:05:31,420 --> 00:05:33,010 You are 55 years old. 65 00:05:34,150 --> 00:05:42,160 So that works the same way, but what if we wanted to use variables because right now we're just doing 66 00:05:42,160 --> 00:05:42,880 this by order. 67 00:05:42,880 --> 00:05:46,660 So whatever comes first gets filled in first in the brackets. 68 00:05:47,500 --> 00:05:50,290 Well, we can do something like name. 69 00:05:52,100 --> 00:05:55,310 And H let's see, can we do that, let's run. 70 00:05:56,590 --> 00:05:57,100 There you go. 71 00:05:57,430 --> 00:05:58,870 That works the same way. 72 00:05:59,770 --> 00:06:06,670 What if we had a specific order, maybe we want this to be H and here to be named? 73 00:06:06,910 --> 00:06:08,740 Well, we can mix those around. 74 00:06:08,980 --> 00:06:17,520 We can say one here and zero here, because in computer science, we always start counting from zero. 75 00:06:17,860 --> 00:06:19,480 So this is zero. 76 00:06:19,780 --> 00:06:20,920 And this is one. 77 00:06:21,130 --> 00:06:26,950 If we had something else in here, let's say a third variable, then this will be. 78 00:06:29,400 --> 00:06:30,690 So if I click run here. 79 00:06:31,820 --> 00:06:34,060 All right, everything is upside down. 80 00:06:35,180 --> 00:06:42,830 Finally, I can just create my own variables if I want to do so, let's say hi, let's say a new name 81 00:06:43,070 --> 00:06:43,700 equals. 82 00:06:45,900 --> 00:06:54,730 Sally and then Age of Sally is going to equal, let's say, one hundred, Sally is very old. 83 00:06:55,260 --> 00:06:58,320 Now, if I do zero, let's say on one here. 84 00:06:59,070 --> 00:07:00,020 OK, quick run. 85 00:07:01,260 --> 00:07:05,730 So now we'll actually get an air tuple index out of range. 86 00:07:05,790 --> 00:07:08,880 Now, we haven't really learned about tuples and ranges. 87 00:07:08,880 --> 00:07:12,110 And this is a little confusing, but a bit of a trick here. 88 00:07:12,510 --> 00:07:16,710 We want to make sure that we add now because we've given. 89 00:07:17,790 --> 00:07:25,080 The actual variable, a value we need to actually say new name here. 90 00:07:26,060 --> 00:07:28,370 And here will be H. 91 00:07:30,090 --> 00:07:31,470 So that if I click run. 92 00:07:32,690 --> 00:07:33,250 There you go. 93 00:07:33,290 --> 00:07:33,920 Hi, Sally. 94 00:07:33,980 --> 00:07:35,390 You are a hundred years old. 95 00:07:37,000 --> 00:07:44,080 As you can see with DOT format, things are a little bit more complicated, you'll still see this and 96 00:07:44,080 --> 00:07:45,190 all python to. 97 00:07:46,100 --> 00:07:52,880 Code still uses the format and you'll see a lot of Python three code bases that still use DOT format 98 00:07:52,880 --> 00:07:54,320 because some people prefer it. 99 00:07:55,070 --> 00:07:57,470 But I would argue that the F. 100 00:07:59,510 --> 00:08:06,320 At the beginning of a formatted string is the way to go because, well, it just makes things so much 101 00:08:06,320 --> 00:08:07,300 easier, right? 102 00:08:07,640 --> 00:08:10,380 Nice and clean, nice and easy. 103 00:08:10,580 --> 00:08:18,860 So I do recommend that you use the formatted string with the F in front and for short, we usually call 104 00:08:18,860 --> 00:08:20,330 this an F string. 105 00:08:20,900 --> 00:08:21,260 All right. 106 00:08:21,770 --> 00:08:22,680 I'll see you in the next one. 107 00:08:23,360 --> 00:08:23,740 Bye bye.