How To Use Print In Python
Python impress() office prints the bulletin to the screen or any other standard output device.
Syntax: print(value(s), sep= ' ', finish = '\n', file=file, flush=flush)
Parameters:
- value(s) : Whatever value, and as many equally you like. Volition be converted to cord earlier printed
- sep='separator' : (Optional) Specify how to split the objects, if there is more than 1.Default :' '
- finish='end': (Optional) Specify what to print at the cease.Default : '\north'
- file : (Optional) An object with a write method. Default :sys.stdout
- flush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (Faux). Default: Simulated
Returns: Information technology returns output to the screen.
Though it is not necessary to pass arguments in the print() office, it requires an empty parenthesis at the finish that tells python to execute the function rather calling it by name. At present, let's explore the optional arguments that tin exist used with the print() function.
Cord Literals
String literals in python's impress statement are primarily used to format or design how a specific string appears when printed using the print() function.
- \n : This string literal is used to add a new blank line while printing a argument.
- "" : An empty quote ("") is used to impress an empty line.
Example:
Python3
print
(
"GeeksforGeeks \n is all-time for DSA Content."
)
Output:
GeeksforGeeks is all-time for DSA Content.
stop= " " statement
The end keyword is used to specify the content that is to be printed at the stop of the execution of the print() function. By default, it is set to "\north", which leads to the alter of line after the execution of print() argument.
Case: Python print() without new line.
Python3
print
(
"GeeksForGeeks is the best platform for DSA content"
)
print
(
"GeeksForGeeks is the all-time platform for DSA content"
, terminate
=
"**"
)
print
(
"Welcome to GFG"
)
Output:
GeeksForGeeks is the all-time platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG
affluent Argument
The I/Os in python are mostly buffered, meaning they are used in chunks. This is where flush comes in as it helps users to decide if they need the written content to exist buffered or not. By default, it is set to fake. If it is set to true, the output will be written equally a sequence of characters one after the other. This process is slow simply because it is easier to write in chunks rather than writing i character at a fourth dimension. To empathize the use example of the flush argument in the impress() part, permit'south take an example.
Example:
Imagine you are building a countdown timer, which appends the remaining time to the aforementioned line every second. Information technology would look something like below:
3>>>ii>>>1>>>Start
The initial code for this would look something like below;
Python3
import
time
count_seconds
=
3
for
i
in
reversed
(
range
(count_seconds
+
1
)):
if
i >
0
:
print
(i, stop
=
'>>>'
)
time.slumber(
1
)
else
:
impress
(
'Start'
)
So, the above code adds text without a trailing newline and and then sleeps for ane 2nd after each text addition. At the end of the countdown, it prints First and terminates the line. If you run the lawmaking every bit it is, it waits for 3 seconds and abruptly prints the entire text at once. This is a waste of three seconds caused due to buffering of the text clamper as shown below:
Though buffering serves a purpose, information technology tin consequence in undesired effects equally shown above. To counter the same event, the flush argument is used with the impress() role. Now, fix the flush argument as truthful and once again see the results.
Python3
import
fourth dimension
count_seconds
=
3
for
i
in
reversed
(
range
(count_seconds
+
1
)):
if
i >
0
:
print
(i, end
=
'>>>'
, flush
=
True
)
time.sleep(
1
)
else
:
print
(
'Start'
)
Output:
Separator
The print() role tin can accept any number of positional arguments. These arguments can exist separated from each other using a "," separator. These are primarily used for formatting multiple statements in a single print() function.
Example:
Python3
b
=
"for"
print
(
"Geeks"
, b ,
"Geeks"
)
Output:
Geeks for Geeks
file Statement
Contrary to pop belief, the print() function doesn't convert the messages into text on the screen. These are washed past lower-level layers of code, that tin read data(message) in bytes. The print() role is an interface over these layers, that delegates the bodily press to a stream or file-similar object. Past default, the print() office is leap to sys.stdout through the file argument.
Example: Python print() to file
Python3
import
io
dummy_file
=
io.StringIO()
print
(
'Hullo Geeks!!'
,
file
=
dummy_file)
dummy_file.getvalue()
Output:
'Hi Geeks!!\north'
Example : Using print() office in Python
Python3
impress
(
"GeeksForGeeks"
)
x
=
5
print
(
"x ="
, x)
print
(
'G'
,
'F'
,
'G'
, sep
=
'')
impress
(
"Python"
, stop
=
'@'
)
impress
(
"GeeksforGeeks"
)
Output:
GeeksForGeeks x = 5 GFG Python@GeeksforGeeks
How To Use Print In Python,
Source: https://www.geeksforgeeks.org/python-output-using-print-function/
Posted by: lightliess1983.blogspot.com
0 Response to "How To Use Print In Python"
Post a Comment