Python print() built-in function

Python print() – Print to Console

Python print() built-in function is used to to print given value to console output or a stream.

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

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

Open a Python shell,

Python Shell with print() function

And write the following print statement.

print("Hello World!")

This print statement prints a string of "Hello World!" to the standard output.

You can also print any other data type. For example let us print a number, say 10.

print(10)
Python print statement in shell

Syntax of print()

The syntax of print() function is

print(*objects, sep=' ', end='\n', file=None, flush=False)

where

ParameterDescription
*objectsRequired
One or more Python objects.
sepOptional
A string value.
This value is used to join the given objects, and then the resulting string is printed to the output.
Default value is single space.
endOptional
A string value.
This value is printed to the output after printing all given objects.
Default value is new line.
fileOptional
A Python object with a write(str) method.
For example, you can write to file with print() function, using this parameter, since file object has write(str) method.
flushOptional
A boolean value. Specifies whether the stream has to be flushed.
Default value is False.

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.

Examples

In the following examples, we shall see how to print values of different datatypes, how to print multiple objects, how to use sep and end parameters of the print() function, etc.

1. Print string value to console output in Python

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

Python Program

print('Hello World')
Run Code Copy

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 an IDE like Visual Studio Code.

Output

The string is printed to the console as shown below.

Hello World

2. Print number to console output in Python

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 Code Copy

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.

3. Print variables to console output in Python

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 Code Copy

Output

pi is 3.14

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

4. Print with a specific separator between values in Python

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 Code Copy

Output

pi is : 3.14

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

5. Print with a specific end value in Python

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 Code Copy

Output

Hello-World.

Video Tutorial for print() in Python

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.

Frequently Asked Questions

1. Which function is used to print a string in Python?

Answer:

python() built-in function can be used to print a string in Python. python() function takes the string as argument, and prints the given argument string to standard output.

print('hello world')
print('hello user')
2. How do you print a number in Python?

Answer:

To print a number in Python, you can use print() built-in function. Pass the number as argument to the print() built-in function and the print() function prints the number to the standard console output.

3. How do you print multiple values with a specific separator between them?

Answer:

To print multiple values with a specific separator between them, you can use print() built-in function with the specific separator passed as argument for the 'sep' parameter

print('apple', 'banana', 'cherry', sep='----')
4. How do you print string value to console output without trailing new line?

Answer:

To print string value to console output without trailing new line in Python, you can use print() built-in function with the empty string passed as argument for the 'end' parameter. The default value for 'end' perimeter in print() function is new line character. By passing an empty string to the 'end' parameter, we override the default behaviour of printing trailing new line by the print() function.

print('apple', end='')

Related Tutorials

Code copied to clipboard successfully 👍