print() in Python
Overview
Python's print() function outputs messages to the console, a standard output device, or a file.
Syntax of print() in Python
The syntax of the print() function is as follows:
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Parameters of print() in Python
These are the parameters accepted by the print() function,
- values: Any number of values can be separated by commas.
- sep: Used to specify how to separate the objects or values if there is more than one. By default, it is a whitespace character.
- end: Used to specify what has to be printed in the end. By default, it is '\n'.
- file: It must be an object with the write(string) method. If this option is not provided, sys.stdout will print objects to the console.
- flush: A Boolean value indicating whether the output is flushed (True) or buffered (False). The default value is False.
sep, end, file, and flush are keyword arguments and are optional parameters in print() in Python.
Return Value of Python print() Function
Return Type: None
print in Python doesn't return any value.
Example:
Output:
What is print in Python?
A fundamental part of programming is printing the output. Different methods exist for printing output in every programming language, whether to the console or files. In Python, the print() function is used for this purpose.
The print() function is specific to Python 3. In Python 2, the print statement is used instead, without parentheses.
Python 2 Example:
Python 3 Example:
In Python 2, print is a statement and does not require parentheses. However, in Python 3, print() is a function and requires parentheses around its arguments.
Flush Argument
The I/O in Python is generally buffered, which means they are used in chunks. Until a display device is ready, data ready to be seen is held in an output buffer in memory or cache. It means that you might see the output from your code later, which makes debugging more difficult.
A flush is handy here since it lets users decide whether they want the written content buffered. It is false by default, meaning the output will generally be buffered.
Turn Learning into Career Growth
More Examples of print() in Python
Example 1: print() with Separator and End Parameters in Python
Output:
Explanation:
- We've passed the sep and end parameters in the above program.
- A separator creates partitions between various objects/items within a print statement. This attribute has, by default, a value of a whitespace character. Here, we've used *** as the separating value.
- The end parameter configures the value you want to put at the end of all provided values. By default, it is \n, i.e., a newline character. In the above example, we've used two newline characters. Hence, the following print() statement prints after two lines.
Example 2: print() with File Parameters
In Python, the output can be written to a file using the file parameter. If the file doesn't exist, it creates one with the same name and then writes to it.
Explanation:
- we have passed the requiredFile file object to the file parameter. The string object 'I'm writing to the file' is printed to the python.txt file.
- Finally, the file was closed using the close() method.
Example 3: Using end Parameters
The end parameter configures the value you want to put at the end of the provided values. By default, it is '\n', i.e., a newline character.
Output:
Explanation
- In the above example, the print() uses the end parameter as &.
- The value of the end parameter can be noticed as when the string1 ends, the next print function prints the string2 after the & .
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Conclusion
- We have just finished this "Print in Python" article. In simple terms, the print() in Python is the function for printing the outputs to the console or file.
- It covers how to print() in Python works and various examples. The article also covers how print() in Python takes several parameters that are of great importance.