Welcome!
In this lecture, I'm going to do that.
Although, there are some differences if you know C and you know how to use printf,
You may skip this lecture entirely.
I'll be using Printf
and its friends extensively in this course.
So it's your call.
OK.
Let's get started.
Let me summarize what you're going to learn. In the first step,
you're going to learn how Printf works. In the second step,
you're going to learn about the escape sequences.
What are they and how they work.
In the third step, you're going to learn how to print the type of any value using Printf.
And in the last step,
I'm going to show you a few examples using Printf.
If you're ready, let's start with the first step.
Alright, let's take a look at this code. Here's a string variable.
And I can print it using Printf. Here, I've used "%q" as a formatter.
I'll talk about what that means in a few minutes.
Just keep that in mind for now.
When you execute this, it will print this.
It prints two double quotes, right?
It's because, "brand" (variable) is empty now.
Let's assign a value to brand to see what it prints,
instead of this empty string.
OK.
Now, I have assigned Google to the brand variable.
Let's see what it prints now.
It prints Google inside double-quotes again.
Now, let's see what happens when I use something else instead of "%q".
Here, this time, as a formatter, I've used "%s" instead of "%q".
Okay now let's see what it prints.
As you can see, instead of printing Google like this, it has printed only Google without double-quotes, right?
The old code was looking like this.
Now let's take a look at the parts of Printf in detail.
Here, I've zoomed in Printf func(tion). Its first argument is the formatting specifier.
But I'm going to call it the formatting text or the formatter text or just the formatter.
OK.
It just determines what to print and how to print. And, its second argument is a value that will replace
the verbs inside its formatting text.
There can be zero or more verbs or values.
This "%q" here is a verb. As you can see, a verb starts with a percent sign, and after that, there is a letter.
For example, "q". "q" here means a quoted-string.
There are lot of verbs that you can use with Printf.
"q" is only one of them and throughout the course, I'll introduce more verbs.
By the way, you can use as many verbs as you want.
Here I'm using only one verb.
That's why I'm passing only the brand variable to Printf.
That is because Printf replaces the verbs with the passed values.
So here, Printf replaces
the "%q" verb with the value of the brand variable.
If there were more verbs then I would need to pass more values.
OK.
"\n" here is an escape sequence.
This "\n" allows you to print a new line. Println prints a new line automatically.
However, Printf doesn't do that.
So that's why you need to add a new line yourself. In the next lecture,
I'm going to show you escape sequences in detail. Let me show you the differences between Println
and Printf.
Let's say that I want to display a message like this.
Imagine that there is a program that processes a lot of files.
Here it prints the number of total operations and the number of successful and failed operations.
So how can I do that using Println and Printf?
Let's start with Println first. So inside Println, ops,
ok and fail variables
print the number of operations. ops prints the number of total operations, and ok and failed print the number
of successful and failed operations (respectively). Assume that these variables are being declared somewhere.
So this is how you can print the below message using Println.
Now, I'm going to print the same message using Printf, this time.
I'm going to type it step by step so you can see how it works.
First of all, let's type its formatting text.
Remember, you always have to type its formatter text first.
It's not like Println. Then I need to type the new line escape sequence "\n".
Remember, Println prints a new line automatically but Printf does not.
So I need to type it ("\n") to print a new line.
By the way, this is not a requirement.
If you don't want, you don't have to print a new line.
OK let's print the total number of operations.
First, I need to type what I want to print inside the formatter text like this.
When I run it like this, it prints this message. As you can see, I didn't pass any values because I didn't
need to.
There are no verbs inside the formatter text right.
So I don't need to pass any value.
You only need to pass values
When there are verbs inside its formatting text.
Now I'm going to type the first verb. "%d" verb here means an integer value.
So it expects an integer value.
Here, I want to print an integer but Printf doesn't know its value yet.
This is just a placeholder. To print any useful output, I need to pass it a value.
Then Printf is going to replace the verb with the passed value.
So now, I'm going to pass it
the ops variable as a value like this.
OK let's run it again. As you can see, Printf replaces
the "%d" with the ops variable, right? And it prints 2350.
Now I'm going to type success inside this formatter text.
Let's run this again.
It prints success, good.
Now let me type another verb here "%d" again.
So this means that Printf will be expecting another integer value. Let's pass the ok variable this time.
When I run this, Printf replaces the second verb, the green one here,
with the ok variable, and it prints 543, cool.
Lastly, let me type another verb.
Now there are three verbs here.
So I need the pass one more value to Printf.
Now I'm going to pass the fail variable to it. OK, let's run it. Now, Printf replaces the third verb,
the red one here, with the fail variable, and it prints 433. As you can see, the number of verbs and the
number of values are the same.
This how printf can replace the verbs with values. And, remember its first arguments should always
be the formatter text.
It determines what and how to print the verbs. And the rest of the arguments are just values to be replaced
with the verbs.
Alright, That's all for this example.
I think that Printf is easier to read and work with compared to Println.
That's why I prefer Printf
most of the time.
Well done.
Now you know the basics of Printf. In the next lecture, you're going to learn about escape sequences.
See you there.