Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python Print Function

The document provides an overview of the print function in Python, which is a built-in feature used to display text or variables on the console. It explains the syntax, parameters, and examples of how to use the print function, including customizing separators and output destinations. Additionally, it addresses frequently asked questions regarding the functionality of the print function.

Uploaded by

millanjainmj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Print Function

The document provides an overview of the print function in Python, which is a built-in feature used to display text or variables on the console. It explains the syntax, parameters, and examples of how to use the print function, including customizing separators and output destinations. Additionally, it addresses frequently asked questions regarding the functionality of the print function.

Uploaded by

millanjainmj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Last Updated on February 27, 2023 by Prepbytes Pages

PREPBYTES


FEBRUARY 27,
2023 ALGORITHMS

ARRAY

BACKTRACKING

C PROGRAMMING
LANGUAGE
Python is one of the most popular programming
languages used in various industries, including C++ PROGRAMMING
data science, machine learning, web LANGUAGE
development, and more. One of the fundamental
CAPGEMINI
features of Python is its ability to output
information to the console or standard output.
CIRCULAR LINKED LIST
The python print function is used to display text
or variables on the screen. It is a built-in function
COMPANY PLACEMENT
in Python, which means it can be used in any PROCEDURE
Python program without requiring any additional
installation. COMPETITIVE CODING

What is Print() Function in COMPUTATIONAL


GEOMETRY
Python?
CSE SUBJECTS
The python print function is a built-in function
that allows you to display text or variables on DATA STRUCTURE
the screen. It is used to output information to
the console or standard output. You can use the DOUBLY LINKED LIST
print function to display messages, variables,
DYNAMIC PROGRAMMING
and even the results of calculations.

GAME THEORY
Syntax of Print Function in Python:

The syntax python print function is GRAPHS

straightforward. Here is the basic syntax:


GREEDY ALGORITHM

HASHING
HEAP
print(object(s), sep=separator, end=end,
INTERVIEW PREPARATION

The python print function takes one or more INTERVIEW TIPS


objects as input and displays them on the
screen. The objects can be strings, numbers, JAVA PROGRAMMING
variables, or even expressions. The objects are LANGUAGE

separated by a separator, which is a comma by


JAVASCRIPT
default. You can change the separator by setting
PROGRAMMING
the sep parameter. The end parameter is used to
LANGUAGE
specify what character should be used to
terminate the output. By default, the end Languages
character is a newline. You can change the end
character by setting the end parameter. The file LINKED LIST
parameter is used to specify the file where the
output will be written. By default, the output is LINKED LIST USING C

written to the console. Finally, the flush


MATHEMATICS
parameter is used to specify whether the output
should be flushed immediately. By default, it is
OPERATING SYSTEM
set to False.
POINTERS
Examples of Print Function in
PYTHON PROGRAMMING
Python
LANGUAGE

Let’s understand python print function with the


QUEUE
help of following examples.

RECURSION
Example 1: Using Separator and End
Parameter SEARCHING

In this example we use print() with separator


SEGMENT TREE
and End parameter in python
SORTING

print("Hello ", "Prepbytes", sep='***',


STACK

print("I'm below two lines")


STRING

TREES
Output :

Recent Articles
Hello ***Prepbytes

I'm below two lines FEBRUARY 7, 2024

Load Balancer
in System
Explanation: In the above program, we’ve Design
1
passed the sep and end parameters.
A separator creates partitions between various
objects/items within a print statement. The FEBRUARY 7, 2024

default value for this attribute is a whitespace Object-


Oriented
character. The separating value, in this case, is
Analysis and
***. 2 Design
The end parameter specifies the value that
should appear at the end of all provided values.
FEBRUARY 6, 2024
It is set to ‘n’ by default, which is a newline
System Design
character. We used two newline characters in Strategy
the preceding example. As a result, the following
print() statement prints after two lines. 3

Example 2: Using File Parameter FEBRUARY 6, 2024

Structured
In this example we use print() with File Analysis and
Parameters Structured
4 Design (SA/SD)

requiredFile = open('python.txt', 'w')


FEBRUARY 5, 2024
print('I m writing to the file', file =
Latency in
requiredFile.close()
System Design

5
Explanation: The file parameter has passed a
requiredFile file object. The string object ‘I’m
FEBRUARY 5, 2024
writing to the file’ is written to the file python.txt.
System Design
Finally, the close() method was used to close the
Life Cycle |
file. SDLC (Design)
6
Example 3: Using End Parameter
In this example we use print() using end
Parameters

string1='Hello'
string2='World'
print(string1,end=' & ')
print(string2)

Output:

Hello & World

Explanation: The print() function in the


preceding example uses the end parameter as
‘&’.The value of the end parameter is visible
because when the string1 ends, the next print
function prints the string2 after the &.

Return Value of Print Function in


Python:

The python print function does not return any


value. It is used to output information to the
console or standard output. However, you can
use the return statement to return a value from a
function. In that case, the value will be displayed
on the screen using the print function.

Parameters of Print Function in Python

Here are the parameters that can be used with


the python print function

value1, value2, …: One or more values or


expressions that you want to print. They
will be separated by the sep parameter if
specified, and followed by the end
parameter if specified.
sep: The separator to use between the
values printed. It defaults to a space
character (‘ ‘).

end: The character to print at the end of the


line. It defaults to a newline character (‘\n’).

file: The file object to write the output to. It


defaults to the console (sys.stdout).

flush: Whether to flush the output buffer


after printing. It defaults to False.

Summary:
The print function in Python is a built-in function
that is used to display text or variables on the
screen. It is one of the fundamental features of
Python and is used in various applications,
including data science, machine learning, web
development, and more. The syntax of the print
function is straightforward, and it takes one or
more objects as input and displays them on the
screen. The print function does not return any
value but is used to output information to the
console or standard output

Frequently Asked
Questions(FAQ):
Sure, here are some additional frequently asked
questions about the python print function :

Q1: How do I print a blank line using the print()


function?
Ans: You can print a blank line using the print()
function by specifying an empty string as the
value to print, like this: print(" ”).

Q2: How do I print a formatted string using the


print() function?
Ans: You can print a formatted string using the
print() function by using the string formatting
syntax. For example, print("The answer is
{}.".format(42)) will output The answer is 42..
Alternatively, you can use f-strings in Python 3.6
and later, like this: print(f"The answer is {42}.").

Q3: How do I redirect the output of the print()


function to a file?
Ans: You can redirect the output of the print()
function to a file by specifying a file object as the
value of the file parameter. For example, with
open("output.txt", "w") as f: print("Hello, world!",
file=f) will write the text "Hello, world!" to a file
named "output.txt" in write mode.

Q4: How do I print a string without a newline


character using the print() function?
Ans: By default, the print() function adds a
newline character (\n) at the end of the string
that it prints. To prevent this, you can specify an
empty string as the value of the end parameter,
like this: print("Hello, ", end=""). This will print
"Hello, " without a newline character.

Q5: How do I print a variable name and its


value using the print() function?
Ans: You can print a variable name and its value
using the print() function by using f-strings and
the vars() function to access the variable’s value
by name.

Q6: Can you change the separator between the


values printed by the print() function?
Ans: Yes, you can change the separator between
the values printed by the print() function by
specifying the sep parameter.

Q7: Can you print text to a file instead of the


console using the print() function?
Ans: Yes, you can print text to a file instead of
the console using the print() function by
specifying the file parameter.

Previous Next

RAM And ROM : Differences Python Power Function


and Types

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

Comment *

Name * Email * Website

Save my name, email, and website in this browser for the next
time I comment.

Post Comment

Related
Post 

You might also like