Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Python print() Method

Python print() function prints the given string or object to the standard output. The standard output is the screen or to the text stream file. Example: Print an integer to Screen Program output. Syntax of print() Method The complete syntax of the print() function is: Method Parameters The …

Lokesh Gupta

July 8, 2021

Python Built-in Functions
Python Basics
Python

Python print() function prints the given string or object to the standard output. The standard output is the screen or to the text stream file.

Example: Print an integer to Screen

print("Learning Python is Easy.")

i = 100
print("The value of i is: ", a)

Program output.

Learning Python is Easy.
The value of i is: 100

Syntax of print() Method

The complete syntax of the print() function is:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Method Parameters

The print() method parameters are:

  • objects – It takes the objects to the printed in the screen. It can take more than one object as method argument.
  • sep – If multiple objects are printed than the objects are separated by sep. The default value of sep is ' '.
  • end – It is printed at the last of the output. The default value is new line character (\n). This is the reason each print() statement displays the output in the new line.
  • file – It must be an object with write(string) method. If not specified, sys.stdout is used which prints output on the screen.
  • flush – It specifies if the output stream has to be forcibly flushed. Default value is False which does not forcibly flush the stream.

Remember that sep, end, file and flush are the keyword arguments. A keyword argument is passed into method with its name.

Method Return Value

The print() method does not return any value. It returns None.

Example: Keyword arguments in print() Method

print("A", "B", "C", "D", sep = ' # ')

Program output.

A # B # C # D

Python print() Method Examples

Let’s look at few examples to understand how to use the print() method.

Example 1: Using print() with sep and end parameters

print("A", "B", "C", "D", sep = ' # ', end = ' Done \n')

Program output.

A # B # C # D Done 

Example 2: Using print() with file parameter

  • The given Python program tries to open the demo.txt in writing mode.
  • If demo.txt file doesn’t exist, a new file is created and opened in writing mode.
  • The string object 'Hello, Python !' is printed to demo.txt file into your computer.
  • Do not forget to close the file using close() method.
sourceFile = open('demo.txt', 'w')
print('Hello, Python !', file = sourceFile)
sourceFile.close()

Program output.

Hello, Python !

Happy Learning !!

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Python Tutorial

  • Python Introduction
  • Python Install in Sublime
  • Python Keywords
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Type Conversion
  • Python Examples

Python Built-in Functions

  • Python abs()
  • Python all()
  • Python any()
  • Python ascii()
  • Python bin()
  • Python input()
  • Python range()
  • Python print()

Python String Functions

  • String capitalize()
  • String count()
  • String endswith()
  • String split()
  • String startswith()

Table of Contents

      • Example: Print an integer to Screen
  • Syntax of print() Method
      • Method Parameters
      • Method Return Value
      • Example: Keyword arguments in print() Method
  • Python print() Method Examples
      • Example 1: Using print() with sep and end parameters
      • Example 2: Using print() with file parameter
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Python input()

Next

Python Print without New Line: Avoid Line Feeds

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap