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.
end str '\n' Keyword argument The string which is printed after all objects have been printed.
file File None Keyword argument The File where the output will be written to. If this argument is NOT provided then data is written to 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 Basic Print Example

In the python file below we make a single call to the print function to output the string Hellow 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 STDOUT.

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

5.2 Printing Multiple Strings Example

In the python file below we are making a single call to the print function, however this time we are passing multiple comma separated strings 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 STDOUT.

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

This document was last updated: