print(): Print Objects To File Function

print

LanguagePython
CategoryFunction
Part OfBuilt In Functions
Named Arguments Count4
Unnamed Arguments Count1/multiple
Official Documentation Print Function

1 Description

The Python print function is used to output data to a file. If no file is specified then the data will be ouput to the standard output. The print function accepts a variable number of strings, to print more than one string simply pass the strings to the print function in a comma separated fashion, see example 2.

2 Prototype

Below is the function prototype for the print function.

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

3 Arguments

Print Function Arguments
Name Type Default Value Category Description
objects any Unnamed argument(s) The object(s) you would like printed.
sep (Separator) str ' ' Keyword argument The string that is printed between each object that is printed. The default value of this parameter is a single space character ' '.
end str '\n' Keyword argument The string which is printed after all objects have been printed. The default value of this parameter is a single newline character \n.
file File None Keyword argument The destination File for the output of the print function. The default value of this parameter is None. When the default value for this parameter is passed to the print function output will be sent to standard output /dev/stdout.
flush Boolean False Keyword argument When flush is True data will be forcibly written to File before the print function returns. When flush is False data will be written to File eventually but not necessarily prior to the return of the print function.

4 Returns

Print Function Returns
Return Type Explanation
None The print function will always return None.

5 Examples

5.1 Print One String

In the python file below we make a single call to the print function to output the string Hello World!.

#!/usr/bin/python3

def main():
    print("Hello World!")

if __name__ == "__main__":
    main()

Now we can run the Python file and observe the string Hello World! being printed to /dev/stdout.

user-1@vm:~/Documents$ ./print_example_1.py
Hello World!
user-1@vm:~/Documents$ 

5.2 Print Multiple Strings

In the Python file below we are making a single call to the print function, however this time we are passing multiple comma separated unnamed arguments to the print function.

#!/usr/bin/python3

def main():
    student_1 = "John"
    student_2 = "Alex"
    student_3 = "Anthony"

    print(student_1, student_2, student_3)

if __name__ == "__main__":
    main()

Now we can run the Python file on the command line and observe how the three names are printed to /dev/stdout.

user-1@vm:~/Documents$ ./print_example_2.py
John Alex Anthony
user-1@vm:~/Documents$ 

5.3 Using A Custom Separator

This example demonstrates the use of the sep keyword argument to specify a custom separator. The separator is the string which is printed between each object that is passed to the print function. The default value of sep is a single space character ' '. In the example below sep is assigned the string ', ' (a comma followed by a space).

#!/usr/bin/python3

def main():
    student_1 = "John"
    student_2 = "Alex"
    student_3 = "Anthony"

    print(student_1, student_2, student_3, sep=", ")

if __name__ == "__main__":
    main()

Now let's run this python program in a terminal and observe the output.

user-1@vm:~/Documents$ ./python_example_3.py
John, Alex, Anthony
user-1@vm:~/Documents$ 

As you can see, the print function now outputs a comma and a space between each name.

5.4 Printing Objects

Objects may define a __str__ method which should return a string object and will be called when an attempt is made to get the string value of an object. When an object is passed to the print function each objects __str__ method is called. Thus you may control how your objects get printed by writing a __str__ method for those objects.

In the example below we define a __str__ method which returns the name of the object when called.

#!/usr/bin/python3

class Fruit:
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name

def main():
    my_favorite_fruit = Fruit("orange")
    not_a_real_fruit = Fruit("tomato")
    print(my_favorite_fruit, not_a_real_fruit)

if __name__ == "__main__":
    main()

And when we run the program we can see that orange tomato is the string that gets printed.

user-1@vm:~/Documents$ ./python_example_4.py
orange tomato
user-1@vm:~/Documents$ 

This document was last updated: