TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Python Print() with Examples
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python BasicPython Tutorials

Python Print() with Examples

Last updated: Oct 22, 2023 4:08 pm
By Meenakshi Agarwal
Share
9 Min Read
Python Print() Explained with Examples
SHARE

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.

Contents
SyntaxPython print() key factsPython print() formatprint(*objects, sep=’ ‘, end=’\n’, flush=False)print(object, …)print(f'{expression}')print(file=f, sep=’ ‘, end=’\n’, flush=False)Print without a newline in PythonExplanationExampleFlush the output buffer while using Python print()

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)
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

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

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

Selenium Python Extent Report Guide

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
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 MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article 7 ways of string concatenation in Python String Concatenation in Python
Next Article SQL Table Creation - The Guide for Beginners SQL Table Creation: The Missing Manual

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x