In python, generally, an inbuilt function print() is used to print the output to the screen or any output device.
The general syntax for print function in python is given below –
Print(value(s), sep = ‘ ’, end = ‘\n’, file = file, flush = flush)
Print() Parameters in Python :
- value(s) – Any value, and as many as you like. Will be converted to a string before printing.
- sep = ‘separator’: This is optional to use, and specifies how to separate different(if more than one is present). The default separator between the objects is: ‘ ’
- end = ‘ ’: This is also optional to use, and used to specify what is to be printed at last. By default ‘\n’ is set to be printed at last.
- file: This is optional to use. An object with a write method. By default it is set to sys.stdout
- flush: It is a Boolean that specifies if the output is flushed or buffered. If the output is flushed, it is true, and if it is buffered, it is false. By default, it is false.
It is not necessary to pass arguments within the print() function.
There are various arguments that can be used inside the print() functions which we will be discussing below –
String Literals
These are used in python to format the way a string is printed. Many times we need to insert an empty line between texts or we want to print text from the next line. So, for these purposes, the following string literals are used –
- \n – After this string literal, the text is printed from the next line.
- “” – An empty quote(“”) is used to print an empty line.
Example –
print(“Yuvayana Tech \n Private Limited”)
Output –
Yuvayana Tech Private Limited
Explanation –
At first, the string ‘Yuvayana Tech’ is printed and after the ‘\n’ string literal is encountered, the next text is printed from the next line.
end = “ ” statement
- This keyword specifies what is to be printed at the end of the print() function. By default, it is ‘\n’ due to which, the line is changed just after the print() function.
We will understand it better with the example given below –
print(“Today is a nice day”) print(“Today is”, end = “Sunday”)
Output –
Today is a nice day
Today is Sunday
Explanation –
In the first line, we haven’t passed any argument. So, the default end statement, which is ‘\n’ is used. So, after printing “today” the next text is printed from the next line because the ‘\n’ string literal is encountered.
In the second line, we have passed Sunday as the argument. So, at last, the text which is given at the end is printed. In this case, Sunday is printed at last.
Flush Argument
The flush statement in python helps the users to decide whether the users want the content to be buffered or not. By default, it is false. When true, the output is written as a sequence of characters. It is easier to write in chunks than to write as a sequence one after the another.
Separator –
It is possible that the print statement contains multiple arguments that need to be printed on the screen. So, there is a separator required to separate between the multiple texts that are given inside the print function. To separate the positional arguments, the keyword argument “sep” is used.
We will understand it better with the example given below –
a = 101 b = 115 print(a ,b ,sep = “-“)
Output –
101-115
File Argument
The print statement is bound to sys.stdout through the file argument which actually does the work of printing the output on the screen. But what we see in the work is that the print statement is responsible, which is not actually true.
So, in this article we have learned about the print statement in Python and the arguments passed in it, to modify or customize the syntax.
Try Python Quizzes: