Print function the print function.
Print a specified message to the screen.
Or any other standard output device.
And this message can be a string or any other object.
This is the syntax for the print function the print function can print an arbitrary number of values
separated by commas.
So while you 1 value 2 when 2 3 and so on I have assigned the value to 2 to this variable X and using
the print function I'm going to print the string X equal to the text but then these double quotes and
then I have separated values the first value value 1 and the second while you value 2 with a comma.
Let us see how this is going to be displayed.
On this screen so x equal to 2.
This is how it got displayed
next.
I'm going to print two more strings here.
So the first string is going to be X the second one is going to be white and in the syntax for the print
function.
We can also specify a separator as a B.
The key word ACP which is the separator and by default it has one space and you can redefine the separator.
Let's say we do not want any spaces between the values X and then white.
So let's change the separator.
I'm going to delete this space.
Let's execute this cell.
So as you can see the space between X and Y has been removed.
Now let's have another separator.
We have two values the String x and the string.
Why are the values the separator.
In this case let's say it is a colon and then execute.
We have the colon as a separator between the values defined in the print function I have used a for
loop to retrieve each of the characters in the string python and after the training say for example
the first character I'm using the print function to print the character be next do for loop retrieves
the second character y and using the print function it prints out the character y in the output similarly
it prints all of the characters in the string Python as you can see a print call is ended by a new line
and we can change this behavior.
So for example we can assign an arbitrary string to the keyboard parameter in say for example let's
use the for loop again in order to access the characters in the string python and then using the print
function.
I'm going to print devalue I so let's print I and the the parameter and this time is now defined to
be a single space let's execute this.
So as you can see instead of printing each character in a new line since we have redefined the parameter
and with space here a single space a space has been included between each of the characters.
This is not a separator here.
It is the space in this case in numbered example is end of the line.
Until now we have talked about parameter separated and the end parameter.
Next we will talk about this parameter file in computer programming.
If a computer program wants to communicate with its environment program that is getting executed wants
to communicate the output to it.
In my admin there are p connected input and output communication channels between the computer program
and the environment.
Also known as standard streams and there are three input output connections.
The first one is the standard input and this is the D5 place read the program listens for information
or input and this could be input from your keyboard next is the standard output the standard output
is the default place for the output to go.
So when you use the print function to bring a string or some value it goes to these standard output
next is the standard error stream.
And just like the standard output and standard input this is the place where error messages go
and standard input is also call is D I N standard output is d d out or UTI standard error is is d d
e at just like you send your output messages from a piece of code to the screen that does your computer
monitor you can also send this text to a file so in that case you do not see the output on your screen.
But this can be seen in a file and let us see how this can be done using the print function.
The print function has this parameter file and by default says DOT is Judy out which is the terminal
or the window in which we are working.
If you want to send the values from the print function into a file that can be done by changing this
parameter so let's discuss about this by default the file parameter is as didi out which means your
output goes to the computer screen and this can be changed.
Say for example you can output the content of a program to a file.
So let's see this.
So here I have created a file object.
If using the open function the built in function open and this is file that I'm going to create print
function dot the X T and I'm opening this in the access mode w.
Now I want to create defined and using the print function.
I want to write this text into this file.
If that is print function dot DST.
And finally after writing the text I'm going to close the file using the close method.
So let's execute this.
So as you can see nothing has been displayed using the print function.
That's because the file parameter is not as Judy's out which is the standard output anymore.
It is now a text file.
So let's open this file print function that the x d text file print function 30 60 and using the print
function.
This is what we have entered into defined the last parameter is this flash.
Which by default is false.
So when you output to a stream from your program it does not go immediately to a file on the terminal
window.
It will be put into a buffer that it will be held until there is enough data to be what outputting for
example a full block to file on disk flushing the output stream forces all output to be written out
so that it is in a disk file or appears on the screen.
And this is occasionally what is needed but it should not be done if it is not needed.
And if you assign the Boolean value true to flush it might slow a program down.