tech beamers
  • Viral Tips 🔥
  • Free CoursesTop
  • TutorialsNew
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
    • Python
    • C
    • Java
    • MySQL
    • Linux
    • Web
    • Android
    • AngularJS
    • Playwright
    • Selenium
    • Agile
    • Testing
    • Automation
    • Best IDEs
    • How-To
    • Technology
    • Gaming
    • Branding
  • Interview & Quiz
    • SQL Interview
    • Testing Interview
    • Python Interview
    • Selenium Interview
    • C Sharp Interview
    • Java Interview
    • Web Development
    • PHP Interview
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
    • Python Interview
    • SQL Query Interview
    • SQL Exercises
    • Selenium Interview
    • Playwright Interview
    • QA Interview
    • Manual Testing
    • Rest API Interview
    • Linux Interview
    • CSharp Interview
    • Python Function Quiz
    • Python String Quiz
    • Python OOP Quiz
    • Python DSA Quiz
    • ISTQB Quiz
    • Selenium Quiz
    • Java Spring Quiz
    • Java Collection Quiz
    • JavaScript Quiz
    • Shell Scripting Quiz
  • ToolsHot
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
    • Online Python Compiler
    • Python Code Checker
    • Python Code Quality
    • Username Generator
    • Insta Password Generator
    • Google Password Generator
    • Free PDF Merger
    • QR Code Generator
    • Net Worth Calculator
tech beamers
Search
  • Viral Tips 🔥
  • Free CoursesTop
  • TutorialsNew
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Interview & Quiz
    • SQL Interview
    • Testing Interview
    • Python Interview
    • Selenium Interview
    • C Sharp Interview
    • Java Interview
    • Web Development
    • PHP Interview
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHot
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
Follow US
© TechBeamers. All Rights Reserved.
Built-in Functions

Python Print() Function Explained

Last updated: Apr 18, 2025 4:03 pm
Meenakshi Agarwal
By
Meenakshi Agarwal
Meenakshi Agarwal Avatar
ByMeenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
Follow:
No Comments
5 months ago
Share
9 Min Read
SHARE

This tutorial covers a most widely-used Python function: print(). It’s a built-in function that displays messages on the console. These messages can be plain text, numbers, or a mix of both—with or without a newline. Let’s explore it in more detail.

Contents
  • Python print() introduction
    • Print() key facts
    • Print() function variations
    • Print without a newline in Python
    • Efficient usage of Python print()
  • Python print() summary
Python Print() Explained with Examples

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.

Python print() introduction

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

print(objects, sep=' ', end='\n', flush=False)
ParameterDescription
objectsA sequence of objects to be printed.
sepA string that separates the objects. The default value is a space.
endA string that is printed at the end of the output. The default value is a newline character (\n).
flushA boolean value that specifies whether to flush the output buffer immediately. The default value is False.
Python print parameters

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.

Print() function variations

The Python print() function has many 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("\nFlushing 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": "Robb Stark", "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.

Efficient usage of 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

Python print() summary

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.

Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.

Enjoy Coding,
TechBeamers

Related

Share This Article
Whatsapp Whatsapp LinkedIn Reddit Copy Link
Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

List of Topics

Stay Connected

FacebookLike
XFollow
YoutubeSubscribe
LinkedInFollow

Continue Reading

  • Python Enumerate FunctionOct 21
  • Python Higher Order FunctionsOct 23
  • Python Ord FunctionOct 28
  • Python Str FunctionDec 27
  • Python Floor FunctionDec 28
  • Python Sorted FunctionJun 1
  • Python Join FunctionJun 10
  • Python Glob Module MethodsJun 23
  • Python Float FunctionAug 3
  • Python Zip FunctionOct 28
View all →

RELATED TUTORIALS

Python floor() function

Python Floor Function

By Meenakshi Agarwal
5 months ago
Python glob method with examples

Python Glob Module Methods

By Meenakshi Agarwal
5 months ago
Python sorted function with examples

Python Sorted Function

By Meenakshi Agarwal
5 months ago
how to use str function in python

Python Str Function

By Meenakshi Agarwal
5 months ago
© TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use