Python print()

Python Print

Almost all of our previous tutorial contains the Python function. But we did not discussed about python print function to the fullest. Now we will learn it. At first we should know about the basic structure of python print function. That is given below;

If you read our tutorial on Python Functions and arguments, then you should probably had the idea about the above function.

The receives a list of undefined variables. So, all the comma separated values will goes under the list values. So if you add more elements separated by comma, you will get a output where all the values are put together separated by space. The following example will guide you about the simple python print function usage.

The output of the following code will be.

So, as many item you want to print, just put them together as arguments.

Using sep Keyword in python print function

If see the example of the previous section, you will notice that that variables are separated with a space. But you can customize it to your own style.

Suppose in the previous code, you want to separate the values using underscore(_). Then you should pass underscore as the value of sep keyword. The following function will illustrate you the idea of using python print sep keyword.

And you will get your desired output like this.

Python print end keyword

The end key of print function will set the string that needs to be appended when printing is done.

By default the end key is set by newline character. So after finishing printing all the variables, a newline character is appended. Hence, we get the output of each print statement in different line. But we will now overwrite the newline character by a hyphen(-) at the end of the print statement. See the following example.

And you will get outputs like the following

Python print to file

In this section we will learn about keyword. Actually file keyword is used for extracting output to a specified file. If you read our previous tutorial Python file operation, then you should know about basic file operation.

So, you have to open a file in writable mode first, then use the file pointer as the value of file keyword in print() function. See the following code to understand the python print file usage.

And you will get the same output as the previous example in an output text file.

That’s all about Python print. Hope that you understood it well. For any further query, feel free to use the comment section. Best of luck.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Adblock
detector