Introduction to Python print()
The Python print() function is a built-in function that prints text either plain or in a specific format with or without the newline to the standard output device, which is typically the console.
The message can be a string, a number, a list, a dictionary, or any other object. The print() function will automatically convert the object to a string before printing it.
Syntax
The syntax of the print() function is as follows:
print(objects, sep=' ', end='\n', flush=False)
Parameter | Description |
---|---|
objects | A sequence of objects to be printed. |
sep | A string that separates the objects. The default value is a space. |
end | A string that is printed at the end of the output. The default value is a newline character (\n). |
flush | A boolean value that specifies whether to flush the output buffer immediately. The default value is False. |
Python print() key facts
Here are some key facts about the Python print() function:
- It can take any number of arguments.
- The arguments are separated by spaces.
- By default, the Python print() function places a newline character (\n) at the end of the output.
- You can set the sep parameter to specify a specific separator between the arguments.
- You can set the end parameter to use a specific character to be printed at the end of the output.
- You can use the flush parameter to flush the output buffer immediately.
Python print() format
The Python print() function has a number of different variations that allow you to format the output. Here are some of the most common variations:
print(*objects, sep=’ ‘, end=’\n’, flush=False)
This is the most general form of the print() function. It takes any number of objects as arguments, and the default separator is a space. The end parameter allows us to set which different characters to print at the end of the output. And, the flush parameter is a boolean to control whether to flush the output buffer or not.
Example:
# Use the sep parameter to specify dash as a separator print("a", "b", "c", sep="-") # Set the end parameter to specify a different character to be printed at the end print("Learn, Python!", end="") # Use the flush parameter to flush the output buffer immediately print("Flushing the output buffer...", flush=True)
print(object, …)
This is a shorthand format of the Python print() function that takes a single object as an argument. The default separator is a space.
Example:
# Print a string print("Learn, Python!") # Print a number print(12345) # Print a list print([1, 2, 3, 4, 5]) # Print a dictionary print({"name": "John Doe", "age": 30})
print(f'{expression}'
)
This form of the print() function uses special string formatting in Python to modify the output. The value of the expression evaluates to a string at runtime and then prints accordingly. You can refer to our f-string tutorial for more details.
Example:
import math # Print the value of pi with two decimal places print(f"The value of pi is {math.pi:.2f}")
The above code results in the following output:
The value of pi is 3.14
print(file=f, sep=’ ‘, end=’\n’, flush=False)
This form of the print() function can be used to write to a file in Python. The file parameter specifies the file object to print to. The other parameters have the same meaning as in the print(*objects, sep=’ ‘, end=’\n’, flush=False) form.
Example:
# Open a file in write mode with open("output.txt", "w") as f: # Print "Learn, Python!" to the file print("Learn, Python!", file=f)
Print without a newline in Python
The Python print variation that helps to print without a newline is the print(object, …, end='')
format. The end parameter specifies the character to be printed at the end of the output. The default value of the end parameter is a newline character (\n).
However, if you set the end parameter to an empty string, then the print() function will not print a newline character at the end of the output.
Here’s an explanation and an example of how to utilize the end parameter to print without a newline:
Explanation
When you pass a string to the end parameter, it will be appended to the end of the printed content instead of the default newline character. This allows you to control which character or string follows the printed text, effectively preventing the next print() statement from starting on a new line.
Example
# Without using the 'end' parameter (default behavior - adds a newline) print("Learn") print("Python") # Using the 'end' parameter to print without a newline print("Learn", end=" ") print("Python")
Output:
Learn Python Learn Python
The first part of the example illustrates the default behavior. Whereas, in the second part, the Python code prints the message without the newline.
Flush the output buffer while using Python print()
To flush the output buffer immediately, we can use the flush parameter of the print() function. It is a boolean value that specifies whether to flush the output buffer immediately.
The default value of the flush parameter is False. However, if we set it to True, then the print() function will flush the output buffer immediately, even if the output buffer is not full.
For example, the below code will print the string “Learn, Python!” and then flush the output buffer immediately:
print("Learn, Python!", flush=True)
This is useful if you want to make sure that the output is printed immediately, without waiting for the output buffer to fill up. Here is another example.
# Create a list of numbers numbers = [1, 2, 3, 4, 5] # Print the numbers one by one, flushing the output buffer after each print for number in numbers: print(number, flush=True)
This code will print the numbers 1, 2, 3, 4, and 5, one by one, with a newline character between each number. Since we set the flush parameter to True, Python will flush the output buffer after each print. It makes the numbers print immediately, without waiting for the buffer to fill up.
Recommended: Generate a List of Random Numbers in Python
Conclusion
Finally, you have finished this tutorial. Let’s sum it up. The Python print() function is a very useful and important tool for showing things on the screen. Its main features let you do basic stuff like showing words and fancy things like putting numbers in sentences using f-strings.
There are different ways to use it, like when you want to put more than one thing together and choose how they are separated or when you want to stop a new line from starting.
You can also use it to send your messages to a special place, like a file, or to find problems in your code. Whether you’re a beginner or an experienced developer, mastering print() is essential for effective debugging and creating user-friendly Python applications.
Here are some additional things to keep in mind. Python print() function can print the following:
- Any type of object, including strings, numbers, lists, dictionaries, and custom objects.
- Multiple objects on the same line.
- Print formatted output.
- Print to a file.
- Control the precision of floating point numbers.
- Flush the output buffer immediately.
We hope this helps! Let us know if you have any questions or feedback. Use the comment box or our contact page to reach out.
Thanks,
TechBeamers