What is print command in Python

Python print() function prints the given object on the screen or other standard output devices.

Signature

print(object(s), sep=separator, end=end, file=file, flush=flush)

object(s): It is an object to be printed. The Symbol * indicates that there may be more than one object.

sep='separator' (optional): The objects are separated by sep. The default value of sep is ' '.

end='end' (optional): it determines which object should be print at last.

file (optional): - The file must be an object with write(string) method. If it is omitted, sys.stdout will be used which prints objects on the screen.

flush (optional): If True, the stream is forcibly flushed. The default value of flush is False.

Return

It does not return any value.

Python print() Function Example 1

The below example shows the working of print().

print("Python is programming language.") x = 7 # Two objects passed print("x =", x) y = x # Three objects passed print('x =', x, '= y')

Output:

Python is programming language. x = 7 x = 7 = y

Explanation:

In the above code, only objects parameter is passed to print() function (in all three print statements).

The end parameter '\n' (newline character) is used to display output in the next line, and it is by default. As we can see, each print statement displays output in the new line.

If the file is saved as sys.stdout, then, the output is printed on the screen.

Here the value of flush is False, so the stream is not forcibly flushed.

Python print() Function Example 2

The below example use print() with separator and end parameters.

x = 7 print("x =", x, sep='00000', end='\n\n\n') print("x =", x, sep='0', end='')

Output:

Next TopicPython Built in Functions

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Like Article

    Python print() function prints the message to the screen or any other standard output device.

    Syntax: 

    print(value(s), sep= ' ', end = '\n', file=file, flush=flush)

    Parameters: 

    • value(s) : Any value, and as many as you like. Will be converted to string before printed
    • sep=’separator’ : (Optional) Specify how to separate the objects, if there is more than one.Default :’ ‘
    • end=’end’: (Optional) Specify what to print at the end.Default : ‘\n’
    • file : (Optional) An object with a write method. Default :sys.stdout
    • flush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False). Default: False

    Return Type: It returns output to the screen.

    Though it is not necessary to pass arguments in the print() function, it requires an empty parenthesis at the end that tells python to execute the function rather calling it by name. Now, let’s explore the optional arguments that can be used with the print() function.

    String Literals

    String literals in python’s print statement are primarily used to format or design how a specific string appears when printed using the print() function.

    • \n : This string literal is used to add a new blank line while printing a statement.
    • “” : An empty quote (“”) is used to print an empty line.

    Example:

    print("GeeksforGeeks \n is best for DSA Content.")

    Output:

    GeeksforGeeks is best for DSA Content.

    end= ” ” statement

    The end keyword is used to specify the content that is to be printed at the end of the execution of the print() function. By default, it is set to “\n”, which leads to the change of line after the execution of print() statement.

    Example: Python print() without new line

    print ("GeeksForGeeks is the best platform for DSA content")

    print ("GeeksForGeeks is the best platform for DSA content", end= "**")

    print("Welcome to GFG")

    Output:

    GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG

    flush Argument

    The I/Os in python are generally buffered, meaning they are used in chunks. This is where flush comes in as it helps users to decide if they need the written content to be buffered or not. By default, it is set to false. If it is set to true, the output will be written as a sequence of characters one after the other. This process is slow simply because it is easier to write in chunks rather than writing one character at a time. To understand the use case of the flush argument in the print() function, let’s take an example.

    Example:

    Imagine you are building a countdown timer, which appends the remaining time to the same line every second. It would look something like below:

    3>>>2>>>1>>>Start

    The initial code for this would look something like below;

    import time

    count_seconds = 3

    for i in reversed(range(count_seconds + 1)):

        if i > 0:

            print(i, end='>>>')

            time.sleep(1)

        else:

            print('Start')

    So, the above code adds text without a trailing newline and then sleeps for one second after each text addition. At the end of the countdown, it prints Start and terminates the line. If you run the code as it is, it waits for 3 seconds and abruptly prints the entire text at once. This is a waste of 3 seconds caused due to buffering of the text chunk as shown below:

    What is print command in Python

    Though buffering serves a purpose, it can result in undesired effects as shown above. To counter the same issue, the flush argument is used with the print() function. Now, set the flush argument as true and again see the results.

    import time

    count_seconds = 3

    for i in reversed(range(count_seconds + 1)):

        if i > 0:

            print(i, end='>>>', flush = True)

            time.sleep(1)

        else:

            print('Start')

    Output:

    https://media.geeksforgeeks.org/wp-content/uploads/20201222163647/Untitled26---Jupyter-Notebook---Google-Chrome-2020-12-22-16-33-02.mp4

    The print() function can accept any number of positional arguments. To separate these positional arguments , keyword argument “sep” is used.

    note: As sep , end , flush , file are keyword arguments their position does not change the result of code.

    Example:

    a=12

    b=12

    c=2022

    print(a,b,c,sep="-")

    Output:

    12-12-2022

    file Argument

    Contrary to popular belief, the print() function doesn’t convert the messages into text on the screen. These are done by lower-level layers of code, that can read data(message) in bytes. The print() function is an interface over these layers, that delegates the actual printing to a stream or file-like object. By default, the print() function is bound to sys.stdout through the file argument. 

    Example: Python print() to file

    import io

    dummy_file = io.StringIO()

    print('Hello Geeks!!', file=dummy_file)

    dummy_file.getvalue()

    Output:

    'Hello Geeks!!\n'

    Example : Using print() function in Python

    print("GeeksForGeeks")

    x = 5

    print("x =", x)

    print('G', 'F', 'G', sep='')

    print("Python", end='@')

    print("GeeksforGeeks")

    Output:

    GeeksForGeeks x = 5 GFG Python@GeeksforGeeks