Python Print – Python print function

by Alex
Python Print - Python print function

As a rule, a program is a data processor. This means that it receives data, processes it, and returns it. One of the easiest ways to show the user the result of a program is to display a message in the console. Often the simplicity of a language is judged by how much code is needed to do this. Let’s look at a few languages and try to print the classic “Hello, World” into the console: Assembler

format PE Console ;
entry start ;
include '' ;
section '.data' data readable writeable ;
hello db 'hello, world!', 0 ;
section '.code' code readable writeable executable ;
start: ;
invoke printf, hello ;
invoke getch ;
invoke ExitProcess, 0 ;
section '.idata' data import readable ;
library kernel, 'kernel32.dll', msvcrt, 'msvcrt.dll'
import kernel, 'ExitProcess', 'ExitProcess'
import msvcrt, printf, 'printf', getch, '_getch


int main()
std::cout << "Hello, World!\n";


public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");

And this is what it looks like in Python:

print('Hello, World!')

Brevity is Python’s strong suit. Let’s explore this function in more detail.

The complete syntax of the print function

The simplest case of this function is as follows:


This code will print an empty line to the console and skip to the next line. Technically speaking, it will print a line feed character that cannot be displayed. It is important, as in all other functions, not to forget about parentheses. Without them, the function will not be called, but the function itself (its object) will be passed. Now let’s move on and write something more meaningful than an empty string:

print('I`m Python master')
# Output:
I`m Python master

As you can see, everything is simple. The full syntax of the print function looks like this:

print(*items, sep='', end='\n', file=sys.stdout, flush=False)

We’ll talk about function parameters later, but it’s worth noting that if you pass a variable as an argument, its value will be printed, and any object will be converted to a string type:

from math import pi, inf
variable = 1
print('variable:', variable)
print('PI:', pi)
print('Infinity:', inf)
print('elipsis:', ...)
print('Data type:', float())
print('Function:', print)
# output:
variable: 1
PI: 3.141592653589793
Infinity: inf
Ellipsis: Ellipsis
Data type: 0.0

Tips: 1. If you list several arguments, the function will print all of them, “glued” into one line, separated by a space (default behavior):

 print('I`m', 'Python', 'master')
# Output:
I`m a Python master

2. Don’t forget to enclose arguments in parentheses: print is a normal function. 3. Don’t forget to enclose strings in single or double quotes. It is best practice to use the same quotes throughout the project. 4. Variables and other non-line-type objects do not need to be quoted.

Parameters print

As mentioned above, the complete syntax of the print function looks like this

print(*items, sep='', end='\n', file=sys.stdout, flush=False)

Let’s deal with the parameters of the function. Here is a list of them:

  • sep is the separator (by default space ” “). The value may be any character or None;
  • end – the character to end the line (by default it is a line break \n). The value may be any character or None;
  • file – the place where the output is sent (by default sys.stdout – console). Occasionally, in addition to the default value, some file is specified here;
  • flush – disable buffering of standard output stream (False by default).

Let’s take a closer look at the parameters.


If you want position arguments to be separated by a character other than a space, pass its value to the named parameter sep:

print('I`m', 'Python', 'master')
print('I`m', 'Python', 'master', sep='\n')
# Output:
I`m a Python master

Here we replaced the space with ‘\n’, a line break, and each new argument was printed on a new line. The character set in the separator can be any character:

print('I`m', 'Python', 'master', sep=' tru-la-la-la ')
print('I`m', 'Python', 'master', sep='|/-\-\+-~`')
# Output:
I`m a trou-la-la-la Python trou-la-la-la master

Here we remind you that if you use the ‘\’ character in a string, you must escape it (‘\\’) or use raw strings (r’\’).


The end parameter is similar to sep, but this character is not inserted between arguments, but after them. By default it is a line break, which is why each new function call is printed on a new line:

print('I`m', 'Python', 'master')
print('I`m', 'Python', 'master')
print('I`m', 'Python', 'master', end=' ')
print('I`m', 'Python', 'master', end='')


By default the print function prints everything to the console, but Python allows you to pass any object with the write(string) method to the file parameter. This makes it possible to use print to write strings to a file:

file = open('result.txt', 'w', encoding='utf-8')
with file as f:
print('I`m', 'Python', 'master', file=f)


When you use output to a file this parameter can turn off buffering. It just means that writing to the file will not happen after exiting the loop as it does here:

import time
file = open('result.txt', 'w', encoding='utf-8')
with file as f:
for _ in range(10):
print('I`m', 'Python', 'master')

but after each function call:

import time
file = open('result.txt', 'w', encoding='utf-8')
with file as f:
for _ in range(10):
print('I`m', 'Python', 'master', flush=True)

Return value

The print() function in Python returns no value (that is, it returns None).

The tasks for the lesson

To reinforce the material, let’s perform some simple tasks together. 1. The classic, as it were: print the string “Hello, World!” Solution: We hope you didn’t have any trouble, because we already did this at the beginning.

print('Hello, World!')
# Output:
Hello, World!

2. Print the same line, but with quotation marks. Solution: All you have to do is use different quotes around and inside the string, otherwise the interpreter won’t understand you.

print(''Hello, World!'')
# Output:
"Hello, World!"

So what do you do if a string contains both single and double quotes? Escape!

print("\"Hello, World! \")
# Output:
"Hello, World!"

3. Ready for the asterisk problem? The user enters an integer two times. After each entry, the program prints what the number equals if it is multiplied by two. The written code should take up one line. Example: Enter the integer 0 0 * 2 = 0 Write an integer 3 3 * 2 = 6 Solution:

[print(variable:=input('Enter an integer '), '*', 2, end=' = '+str(int(variable)*2)+'\n') for i in range(2)]
# Output:
Enter an integer 0
0 * 2 = 0
Type an integer 3
3 * 2 = 6

Related Posts