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