Python – Print to Console – print()

Print to Console in Python

To print strings to console or echo some data to console output, use Python inbuilt print() function.

print() function can take different type of values as argument(s), like string, integer, float, etc., or object of a class type.

Following is a simple demonstration of how to use print() function in a Python shell.

>>> print("Hello World! Welcome to Python Examples.")
Hello World! Welcome to Python Examples.

>>> print(10)
10
Run this program

As we have already mentioned, you can pass in a string object, number or any other datatype to print() function as argument, to print to the console.

Also, please note that, you can pass one or more objects or values as arguments to the print() function. print() function can handle all those arguments using separator and end parameters to print all them to the console. We shall look into these parameters of print() function, in detail, in the following examples.

But for now, remember that end parameter has a default value of new line character. And this value is appended at the end of printing. Or you can override the default end character by passing a value for end parameter.

separator parameter has a default value of single space. When you provide multiple values for printing, the separator will be used to join those values and print to the console.

Python - Print to Console

Example 1: Print String to Console

In this example, we will print a string to console output. Pass string as argument to the print() function.

Python Program

print('Hello World')
Run this program

Yeah! That one print statement is all of the program. Run it from your command prompt or terminal using python command. Or you can run it from your Jupyter Notebook.

Output

The string is printed to the console as shown below.

Hello World

Example 2: Print number to console

In this example, we will pass integer (number) as argument to print() function and print the number to the console.

Python Program

print(526)
Run this program

Output

526

Any datatype passed to the print function is implicitly converted to string and printed to the console. Hence, the int we passed as argument to print() is first converted to string, and then print to the console.

Example 3: Print Variables to Console

You can provide multiple values to print() function as arguments. They will be printed to console with single space as a default separator. Again, you can specify your own separator, and we shall see that in our next example.

Python Program

x = 'pi is'
y = 3.14
print(x,y)
Run this program

Output

pi is 3.14

Please note that space is printed out to console, between x and y.

Example 4: Python print() with a Specific Separator

You can pass a string for named parameter sep to override the default separator.

Python Program

x = 'pi is'
y = 3.14
print(x, y, sep=' : ')
Run this program

Output

pi is : 3.14

x and y have been printed to the console with the specified separator.

Example 5: Python Print with a Specific End

We already know that we can specify an ending value for print() function. New line character is the default ending for print() function in Python. You can override this by passing a string to the named parameter end as shown in the below Python program.

Python Program

print('Hello', end='-')
print('World', end='.\n')
Run this program

Output

Hello-World.

Video Tutorial

Python Print to Console

Summary

In this tutorial of Python Examples, we learned how to print a message to console, a variable or any other datatype to the console, using Python print() builtin function with the help of well detailed Python programs.