WEBVTT 0 00:02.460 --> 00:08.250 Welcome! 1 00:08.250 --> 00:11.350 In this lecture, I'm going to do that. 2 00:12.270 --> 00:16.550 Although, there are some differences if you know C and you know how to use printf, 3 00:16.620 --> 00:18.930 You may skip this lecture entirely. 4 00:19.470 --> 00:20.460 I'll be using Printf 5 00:20.520 --> 00:23.220 and its friends extensively in this course. 6 00:23.340 --> 00:24.750 So it's your call. 7 00:24.810 --> 00:25.290 OK. 8 00:25.530 --> 00:26.910 Let's get started. 9 00:26.910 --> 00:30.300 Let me summarize what you're going to learn. In the first step, 10 00:30.420 --> 00:34.540 you're going to learn how Printf works. In the second step, 11 00:34.550 --> 00:36.980 you're going to learn about the escape sequences. 12 00:36.980 --> 00:38.610 What are they and how they work. 13 00:39.480 --> 00:44.380 In the third step, you're going to learn how to print the type of any value using Printf. 14 00:45.310 --> 00:46.220 And in the last step, 15 00:46.350 --> 00:49.340 I'm going to show you a few examples using Printf. 16 00:49.740 --> 00:52.080 If you're ready, let's start with the first step. 17 00:53.390 --> 00:58.340 Alright, let's take a look at this code. Here's a string variable. 18 00:58.440 --> 01:04.120 And I can print it using Printf. Here, I've used "%q" as a formatter. 19 01:04.140 --> 01:07.220 I'll talk about what that means in a few minutes. 20 01:07.230 --> 01:08.840 Just keep that in mind for now. 21 01:10.180 --> 01:13.390 When you execute this, it will print this. 22 01:13.390 --> 01:16.180 It prints two double quotes, right? 23 01:16.210 --> 01:18.890 It's because, "brand" (variable) is empty now. 24 01:19.260 --> 01:22.200 Let's assign a value to brand to see what it prints, 25 01:22.270 --> 01:23.790 instead of this empty string. 26 01:23.830 --> 01:24.520 OK. 27 01:25.090 --> 01:28.060 Now, I have assigned Google to the brand variable. 28 01:28.510 --> 01:29.830 Let's see what it prints now. 29 01:30.670 --> 01:33.690 It prints Google inside double-quotes again. 30 01:33.700 --> 01:37.640 Now, let's see what happens when I use something else instead of "%q". 31 01:37.660 --> 01:42.960 Here, this time, as a formatter, I've used "%s" instead of "%q". 32 01:42.970 --> 01:45.890 Okay now let's see what it prints. 33 01:46.300 --> 01:53.460 As you can see, instead of printing Google like this, it has printed only Google without double-quotes, right? 34 01:54.900 --> 01:57.420 The old code was looking like this. 35 01:57.420 --> 02:02.810 Now let's take a look at the parts of Printf in detail. 36 02:02.810 --> 02:08.680 Here, I've zoomed in Printf func(tion). Its first argument is the formatting specifier. 37 02:09.110 --> 02:14.440 But I'm going to call it the formatting text or the formatter text or just the formatter. 38 02:14.540 --> 02:15.660 OK. 39 02:15.680 --> 02:22.770 It just determines what to print and how to print. And, its second argument is a value that will replace 40 02:22.770 --> 02:25.760 the verbs inside its formatting text. 41 02:25.830 --> 02:30.800 There can be zero or more verbs or values. 42 02:30.800 --> 02:37.730 This "%q" here is a verb. As you can see, a verb starts with a percent sign, and after that, there is a letter. 43 02:38.270 --> 02:43.890 For example, "q". "q" here means a quoted-string. 44 02:43.950 --> 02:46.590 There are lot of verbs that you can use with Printf. 45 02:46.710 --> 02:51.930 "q" is only one of them and throughout the course, I'll introduce more verbs. 46 02:52.140 --> 02:55.330 By the way, you can use as many verbs as you want. 47 02:55.470 --> 02:57.210 Here I'm using only one verb. 48 02:57.390 --> 03:00.500 That's why I'm passing only the brand variable to Printf. 49 03:00.690 --> 03:05.180 That is because Printf replaces the verbs with the passed values. 50 03:05.610 --> 03:07.240 So here, Printf replaces 51 03:07.290 --> 03:11.120 the "%q" verb with the value of the brand variable. 52 03:11.640 --> 03:15.840 If there were more verbs then I would need to pass more values. 53 03:16.320 --> 03:17.210 OK. 54 03:17.230 --> 03:20.310 "\n" here is an escape sequence. 55 03:20.310 --> 03:26.690 This "\n" allows you to print a new line. Println prints a new line automatically. 56 03:26.730 --> 03:28.810 However, Printf doesn't do that. 57 03:28.830 --> 03:33.690 So that's why you need to add a new line yourself. In the next lecture, 58 03:33.690 --> 03:40.140 I'm going to show you escape sequences in detail. Let me show you the differences between Println 59 03:40.140 --> 03:40.950 and Printf. 60 03:40.950 --> 03:45.340 Let's say that I want to display a message like this. 61 03:45.550 --> 03:49.680 Imagine that there is a program that processes a lot of files. 62 03:49.730 --> 03:55.370 Here it prints the number of total operations and the number of successful and failed operations. 63 03:56.440 --> 03:59.690 So how can I do that using Println and Printf? 64 04:00.040 --> 04:05.220 Let's start with Println first. So inside Println, ops, 65 04:05.280 --> 04:07.040 ok and fail variables 66 04:07.090 --> 04:14.960 print the number of operations. ops prints the number of total operations, and ok and failed print the number 67 04:14.960 --> 04:21.490 of successful and failed operations (respectively). Assume that these variables are being declared somewhere. 68 04:21.500 --> 04:24.890 So this is how you can print the below message using Println. 69 04:24.950 --> 04:30.220 Now, I'm going to print the same message using Printf, this time. 70 04:30.300 --> 04:33.920 I'm going to type it step by step so you can see how it works. 71 04:35.450 --> 04:38.520 First of all, let's type its formatting text. 72 04:38.540 --> 04:42.440 Remember, you always have to type its formatter text first. 73 04:42.440 --> 04:49.720 It's not like Println. Then I need to type the new line escape sequence "\n". 74 04:49.730 --> 04:53.690 Remember, Println prints a new line automatically but Printf does not. 75 04:53.740 --> 04:56.020 So I need to type it ("\n") to print a new line. 76 04:56.020 --> 04:57.970 By the way, this is not a requirement. 77 04:58.120 --> 05:00.720 If you don't want, you don't have to print a new line. 78 05:01.240 --> 05:03.550 OK let's print the total number of operations. 79 05:03.550 --> 05:09.060 First, I need to type what I want to print inside the formatter text like this. 80 05:10.090 --> 05:18.010 When I run it like this, it prints this message. As you can see, I didn't pass any values because I didn't 81 05:18.010 --> 05:18.850 need to. 82 05:18.910 --> 05:21.920 There are no verbs inside the formatter text right. 83 05:21.970 --> 05:24.280 So I don't need to pass any value. 84 05:24.280 --> 05:25.940 You only need to pass values 85 05:25.960 --> 05:29.500 When there are verbs inside its formatting text. 86 05:29.500 --> 05:35.350 Now I'm going to type the first verb. "%d" verb here means an integer value. 87 05:35.520 --> 05:37.920 So it expects an integer value. 88 05:38.160 --> 05:43.060 Here, I want to print an integer but Printf doesn't know its value yet. 89 05:43.230 --> 05:48.910 This is just a placeholder. To print any useful output, I need to pass it a value. 90 05:49.020 --> 05:53.210 Then Printf is going to replace the verb with the passed value. 91 05:53.520 --> 05:54.840 So now, I'm going to pass it 92 05:54.840 --> 05:57.550 the ops variable as a value like this. 93 05:59.050 --> 06:03.560 OK let's run it again. As you can see, Printf replaces 94 06:03.560 --> 06:10.490 the "%d" with the ops variable, right? And it prints 2350. 95 06:10.500 --> 06:14.830 Now I'm going to type success inside this formatter text. 96 06:14.840 --> 06:16.560 Let's run this again. 97 06:16.580 --> 06:18.400 It prints success, good. 98 06:18.820 --> 06:22.290 Now let me type another verb here "%d" again. 99 06:22.310 --> 06:27.630 So this means that Printf will be expecting another integer value. Let's pass the ok variable this time. 100 06:27.700 --> 06:30.180 So this means that Printf will be expecting another integer value. Let's pass the ok variable this time. 101 06:30.330 --> 06:35.040 When I run this, Printf replaces the second verb, the green one here, 102 06:35.160 --> 06:39.810 with the ok variable, and it prints 543, cool. 103 06:40.020 --> 06:42.810 Lastly, let me type another verb. 104 06:42.870 --> 06:44.580 Now there are three verbs here. 105 06:44.580 --> 06:48.410 So I need the pass one more value to Printf. 106 06:48.420 --> 06:56.330 Now I'm going to pass the fail variable to it. OK, let's run it. Now, Printf replaces the third verb, 107 06:56.410 --> 07:03.160 the red one here, with the fail variable, and it prints 433. As you can see, the number of verbs and the 108 07:03.160 --> 07:05.260 number of values are the same. 109 07:05.260 --> 07:10.990 This how printf can replace the verbs with values. And, remember its first arguments should always 110 07:10.990 --> 07:12.710 be the formatter text. 111 07:12.760 --> 07:18.820 It determines what and how to print the verbs. And the rest of the arguments are just values to be replaced 112 07:18.820 --> 07:22.680 with the verbs. 113 07:22.760 --> 07:25.230 Alright, That's all for this example. 114 07:25.280 --> 07:29.700 I think that Printf is easier to read and work with compared to Println. 115 07:29.900 --> 07:31.360 That's why I prefer Printf 116 07:31.400 --> 07:34.190 most of the time. 117 07:36.980 --> 07:37.490 Well done. 118 07:37.610 --> 07:42.790 Now you know the basics of Printf. In the next lecture, you're going to learn about escape sequences. 119 07:42.800 --> 07:43.340 See you there.