The print() Function
A program that does clever work but shows you nothing is not much use. print() is how Python shows things on the screen. In short, it displays all sorts of content for you: text (which programmers call a string), numbers, and other kinds of values you will meet as you go along. To start with, remember one rule — text goes inside single or double quotes, numbers do not.
1Why do we call it a function?
print() is also the easiest place to meet two words you will hear again and again: function and argument. Take them one at a time, starting with function.
( ) have a proper name in programming: parentheses. From now on, whenever this course says parentheses, it simply means these round brackets ( ). Same thing, proper name. Your textbook and your exam paper will use this word too, so it is worth getting used to.So, why is print() called a function? Because of the parentheses. That is the simple answer, and for now it is enough:
( ) right after it — print(), input(), len(), type() — is a function. The ( ) is the clue.This is a simple, everyday definition. The full definition needs ideas you have not learnt yet, so we will use this one for now. It is not wrong. It is just not the whole story. When you learn the rest later, this rule will still hold.
And it works for every function, not just print(). Any time you see a new word with parentheses after it, you already know what it is.
2The parts of one line
Here is the most famous line in programming. Tap each part of it. New words are easier to learn when you can point at what they mean.
So, to show something in Python, you put it inside the parentheses of print(). And the thing you put inside has a name. It is called an argument.
3Type your first line
>>> is what you type. The line under it is Python's answer.Type this at the prompt and press Enter. Python answers straight away:
Look at what did not appear: the quotes. You typed 'Hello, world!' and Python showed Hello, world!. The quotes only tell Python “this is text”. They are not part of the message, so they are not printed.
No Python on your computer yet? Then use the prompt below. It is a real Python prompt, running here in the page. Type a line, press Enter, and Python answers — exactly as it would in your own terminal.
4Printing more than one thing
You can give print() several arguments at once. Just separate them with commas. Python prints them in order and puts one space between them. You do not have to add that space yourself.
Notice the spaces between Ramesh, Kumar and 17. You did not type them. Python added them. Try it at the prompt below — add an argument of your own, and see what changes.
An empty print() — with nothing inside — is not an error. You asked it to show nothing, so it shows nothing and moves to the next line. This is how you print a blank line.
5Every print() starts a new line
When print() finishes, it moves down to a new line. So two print() lines always give you two lines of output. This one is easier to see in script mode: the two lines of code run together, and the output arrives as a block, with no >>> prompts sitting in between.
print('First')
print('Second')First Second
print() with two arguments: print('First', 'Second'). The number of output lines is simply the number of times you called print().6Recap
print() is a function — we know that because of the parentheses ( ). What you put inside them is called an argument, and that is what gets shown. Several arguments, separated by commas, print on one line with a space between them. No argument prints a blank line. And every print() ends by starting a new line.How do we know that print is a function?
What does print('Sara', 'Ali') show?