Python print() function: Format, Code, Arguments, Examples

Python print() Function: In this tutorial, we will learn about Python's print() function with its format, syntax code, arguments, and different examples. By IncludeHelp Last updated : December 17, 2023

Printing text/values is a very basic requirement in any programming language, the first thing that you learn in any programming is how to print something on the output screen. If you are learning programming in Python, the first thing that you must learn – How to print in Python? In this tutorial, we are covering everything related to Python's print() statement. This is a complete guide related to the Python print statement.

Python print() Function

The print() is an inbuilt function in Python programming, it is used to print the text, and values, i.e., objects on the standard output device screen (or, to the text stream file). It is a very basic function that every Python programmer must know.

Python print(): General Syntax

The general syntax of the print() function is as follows:

print(objects)

Python print(): Full Syntax

The complete syntax of the print() function with all arguments is as follows:

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

Python print(): Function Argument(s)

The list of the arguments that the print() function can accept:

  • objects: The value or the variables/objects to be printed on the screen, multiple objects can be passed separating by the commas (object1, object2, ..., objectN).
  • sep: It's an optional parameter and is used to specify the separator between the arguments, The default value is space (' ').
  • end: It's also an optional parameter and is used to specify the value to be printed at the last. The default value is a newline character ('\n').
  • file: It's also an optional parameter and is used to specify the file name where we can write the argument values. The default value is sys.stdout.
  • flush: It's also an optional parameter and is used to flush the stream. The default value is "False".

Python print(): Return Type/Value

The return type of print() function is NoneType, it returns nothing.

Consider the below code, showing the returns type of the print() function. To print the return type of print(), you can use the type() function.

# Example to print the retun type/value
# of print() function

print(type(print("Hello, world!")))

The output of the above code will be:

Hello, world!
<class 'NoneType'>

Python print() Function: More Examples

To work with the print() function in Python, practice the below-given Python examples with code, output, and explanation. These examples help you to understand various techniques for printing different objects.

print() Function Example 1: Print single value

In this example, we will print the single value of the different types.

# Python print() Function Example 1
# Print single value

print("Hello, world!")  # string
print(10)  # int
print(123.456)  # float
print([10, 20, 30])  # list
print((10, 20, 30))  # set
print({"a": "apple", "b": "banana", "c": "cat"})  # dictionary

Output

Hello, world!
10
123.456
[10, 20, 30]
(10, 20, 30)
{'a': 'apple', 'b': 'banana', 'c': 'cat'}

print() Function Example 2: Print multiple values

The multiple values (objects) can also be printed using the print() function. In this example, we are printing multiple values within a single print statement.

# Python print() Function Example 2
# Print multiple values

print("Hello", "world!")
print("Anshu Shukla", 21)
print("Alex", 23, 98.50, [86, 78, 90])
print("Dictionary", {"a": "apple", "b": "banana"})
print("List", [10, 20, 30])

Output

Hello world!
Anshu Shukla 21
Alex 23 98.5 [86, 78, 90]
Dictionary {'a': 'apple', 'b': 'banana'}
List [10, 20, 30]

print() Function Example 3: Print messages and variables together

Any message and variables are considered as objects, they can be easily printed separating them by the commas (,). Consider the below example demonstrating the same.

# Python print() Function Example 3
# Print messages and variables together

# Variables
name = "Anshu Shukla"
age = 21
marks = [80, 85, 92]
perc = 85.66

# Printing
print("Name:", name)
print("Age:", age)
print("Marks:", marks)
print("Percentage:", perc)

Output

Name: Anshu Shukla
Age: 21
Marks: [80, 85, 92]
Percentage: 85.66

print() Function Example 4: Print multiple objects of mixed type values

Learn how you can print multiple objects along with the different types of values in a single print statement.

# Python code to demonstrate the example of
# print() function without using
# optional parameters

# printing strings
print("Hello, world!")
print("How are you?")
print("India", "USA", "Russia", "Israel")
print()

# printing mixed value
print("Mike", 21, "USA", 65.50)
print([10, 20, 30])  # list
print({"Mike", 21, "USA", 123.5})  # set
print(123, "Hello", [10, 20, 30])  # number, string, list
print()

# printing text or/and variables
name = "Mike"
age = 21
con = "USA"
w = 65.50

print(name, age, con, w)
print("Name:", name, "Age:", age, "Country:", con, "Weight:", w)

Output

Hello, world!
How are you?
India USA Russia Israel

Mike 21 USA 65.5
[10, 20, 30]
{'USA', 123.5, 'Mike', 21}
123 Hello [10, 20, 30]

Mike 21 USA 65.5
Name: Mike Age: 21 Country: USA Weight: 65.5

Working with escape characters in Python print() function

The following are the escape characters that can be used within the print() function to print special values or to format the output.

Escape characterDescription
\' Prints single quote
\\ Prints backslash
\n Prints a new line
\r Prints carriage return
\t Prints tab space
\b Prints backspace
\f Prints form feed
\ooo Prints octal value
\xhh Prints hexadecimal value

Example

# Example of escape characters

# \'
print("Hello 'Alex!'")
# \\
print("Hello \\Alvin!")
# \n
print("Hello \nBobby!")
# \r
print("Hello \rCristina!")
# \t
print("Hello\tDen!")
# \b
print("Hello\bEmily!")
# \f
print("Hello\fFrankel!")

# \ooo
print("Hello in octal: \110\145\154\154\157")
# \xhh
print("Hello in hexadecimal: \x48\x65\x6c\x6c\x6f")

Output

Hello 'Alex!'
Hello \Alvin!
Hello 
Bobby!
Cristina!
Hello   Den!
HellEmily!
Hello
     Frankel!
Hello in octal: Hello
Hello in hexadecimal: Hello

Python print() function with sep parameter

sep parameter stands for separator, it uses with the print() function to specify the separator between the arguments. The default value is space i.e. if we don't use sep parameter – then the value of the arguments is separated by the space.

Example

In the below program, we will learn how to use sep parameter with the print() function?

# Python code to demonstrate the example of 
# print() function with sep parameter

print("Separated by ','")
print("Mike", 21, "USA", 65.50, sep=',')
print("Separated by ' # '")
print("Mike", 21, "USA", 65.50, sep=' # ')
print()

name = "Mike"
age = 21
con = "USA"
w = 65.50

print("Separated by ','")
print(name, age, con, w, sep=',')
print("Separated by '\n'")
print("Name:", name, "Age:", age, "Country:", con, "Weight:", w, sep='\n')

Output:

Separated by ','
Mike,21,USA,65.5
Separated by ' # '
Mike # 21 # USA # 65.5

Separated by ','
Mike,21,USA,65.5
Separated by '
'
Name:
Mike
Age:
21
Country:
USA
Weight:
65.5

Python print() function with end parameter

Python's print() function comes with a parameter called end. By default, the value of this parameter is '\n', i.e., the new line character. We can specify the string/character to print at the end of the line.

Example

In the below program, we will learn how to use the end parameter with the print() function?

# Python code to demonstrate the example of 
# print() function with end parameter

print("Ended by none i.e. removing default sep value")
print("Hello,", end='')
print("world", end='')


print("Ended by '###\\n'")
print("Mike", 21, "USA", 65.50, end='###\n')
print("Ended by '-END-\\n'")
print("Mike", 21, "USA", 65.50, end='-END-\n')
print()

name = "Mike"
age = 21
con = "USA"
w = 65.50

print("Ended by 'FINISH\\n'")
print(name, age, con, w, end='FINISH\n')
print("Ended by '@@@@'")
print("Name:", name, "Age:", age, "Country:", con, "Weight:", w, end='@@@')

Output:

Ended by none i.e. removing default sep value
Hello,worldEnded by '###\n'
Mike 21 USA 65.5###
Ended by '-END-\n'
Mike 21 USA 65.5-END-

Ended by 'FINISH\n'
Mike 21 USA 65.5FINISH
Ended by '@@@@'
Name: Mike Age: 21 Country: USA Weight: 65.5@@@

Python print() function with file parameter

file parameter is used with print() function to write the value of given arguments to the specified file. If it is not specified, by default the value is written to system.stdout. It can be used to create log the values i.e. to keep the track to the statements, logics, etc.

Example

In the below program, we will learn how to use the file parameter with the print() function?

# Python code to demonstrate the example of 
# print() function with file parameter

import sys

print("Printing to sys.stderr")
print("Hello, world!", file = sys.stderr)

print("Printing to an external file")
objF = open("logs.txt", "w")
print("How are you?", file = objF)

objF.close()

Output:

Printing to sys.stderr
Hello, world!
Printing to an external file

--- logs.txt ---
How are you?

Python print() function with flush parameter

Python's print() method as an exclusive attribute namely, flush which allows the user to decide if he wants his output to be buffered or not. The default value of this is False meaning the output will be buffered.

Example

In the below program, we will learn how to use the flush parameter with the print() function?

# Python code to demonstrate the example of 
# print() function with flush parameter

from time import sleep

# output is flushed here
print("Hello, world!", end='', flush= True)
sleep(5)
print("Bye!!!")

# output is not flushed here
print("IncludeHelp", end='')
sleep(5)
print("Okay!!!")

Output:

Hello, world!Bye!!!
IncludeHelpOkay!!!

See the output – "Hello, world" and "Bye!!!" are printing correctly because before sleep(5) the print() is flushing but "IncludeHelp" and "Okay!!!" are printing together, because print() is not flushing.

Python print() function with string modulo operator (%)

The string modulo operator (%) is used with print() statement for string formatting. It can be used as C-like printf() statement.

Example

# Example of using string modulo operator(%)
# with print() function

name = "Alex"
age = 21
perc = 89.99

# printing all values
print("Name : %s, Age : %d, Percentage : %.2f" % (name, age, perc))

# integer padding
print("%5d\n%5d\n%5d" % (1, 11, 111))

# printing octal, hex values
print("Octal : %o, Hex : %x" % (891, 891))

# printing character values
print("A : %c, B : %c" % ("A", 66))

Output:

Name : Alex, Age : 21, Percentage : 89.99
    1
   11
  111
Octal : 1573, Hex : 37b
A : A, B : B

References

You may like these resources:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.