***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Tuesday, April 27, 2021

Displaying Output with the print Function

The most fundamental
built-in function of Python is the print function, which displays output on the
screen.



Here is an example of a
statement that executes the print function:



print('Hello world')



In
interactive mode:



>>>
print('Hello world')



Hello world



>>> 



When you call the print
function, you type the word print, followed by a set of parentheses. Inside the
parentheses, you type an argument, which is the data that you want
displayed on the screen.



Using
Script:



Program
to display name and address on the computer screen



program name: output.py



print(“Ashraf”)



print(“TTWRDC,Maripeda”)


More
About Data Output

Output can be formatted
in different ways.

1. Suppressing the print
Function’s Ending Newline

The print function
normally displays a line of output.

Example:

print('One')

print('Two')

print('Three')

Each of the statements
shown here displays a string and then prints a newline character.

Output:

One

Two

Three

If the print function
should not start a new line of output when it finishes displaying its output, pass
the special argument end=' ' to the function.

print('One', end=' ')

print('Two', end=' ')

print('Three')

This specifies that the
print function should print a space instead of a newline character at the end
of its output.

Here is the output of
these statements:

One Two Three

To print anything at
the end of its output, not even a space. If that is the case, pass the argument
end='' to the print function, as shown in the following code:

print('One', end='')

print('Two', end='')

print('Three')

This specifies that the
print function should print nothing at the end of its output. Here is the
output of these statements:

OneTwoThree

2. Specifying an Item
Separator

When multiple arguments
are passed to the print function, they are automatically separated by a space
when they are displayed on the screen.

>>>
print('One', 'Two', 'Three')

One Two Three

If space is not to be
printed between the items, pass the argument sep='' to the print function, as
shown here:

>>>
print('One', 'Two', 'Three', sep='')

OneTwoThree

To specify a character
other than the space to separate multiple items in the output use sep=’*’

>>>
print('One', 'Two', 'Three', sep='*')

One*Two*Three

3. Escape Characters

An escape character is
a special character that is preceded with a backslash (\), appearing inside a
string literal. When a string literal that contains escape characters is
printed, the escape characters are treated as special commands that are
embedded in the string.











































































Some of Python’s
escape characters


Examples:



Ex \n:



print('One\nTwo\nThree')



When this statement
executes, it displays



One



Two



Three



Ex \t:



print('Mon\tTues\tWed')



print('Thur\tFri\tSat')



The output is



Mon     Tues     Wed



Thur     Fri        Sat




Ex \' and \":

The \' and \"
escape characters can be used to display quotation marks.



print("Your
assignment is to read \"Hamlet\" by tomorrow.")



print('I\'m ready to
begin.')



These statements
display the following:



Your assignment is to
read "Hamlet" by tomorrow.



I'm ready to begin.




Ex \\:

The \\ escape character
is used to display a backslash, as shown in the following:



print('The path is
C:\\temp\\data.')



This statement will
display



The path is
C:\temp\data.




4. Displaying Multiple
Items with the + Operator



When the + operator is used
with two strings, it performs string concatenation.



print('This is ' + 'one
string.')



This statement will
print



This is one string.



5. Formatting Numbers



When a floating-point
number is displayed by the print function, it can appear with up to 12
significant digits.



Python provides the
built-in format function, which can be used to round the decimal places. The format
specifier
is a string that contains special characters specifying how the
numeric value should be formatted.



format function has two
arguments: a numeric value and a format specifier.



format(12345.6789, '.2f
')



The first argument,
which is the floating-point number 12345.6789, is the number that we want to
format. The second argument, which is the string '.2f', is the format
specifier.



Here is the meaning of
its contents:



• The .2 specifies the
precision. It indicates that we want to round the number to two decimal places.



• The f specifies that
the data type of the number we are formatting is a floating-point number.



output: 12345.68



Formatting in
Scientific Notation



To display
floating-point numbers in scientific notation, use the letter e or the letter E
instead of f.



>>>
print(format(12345.6789, 'e'))



1.234568e+04



>>> print(format(12345.6789,
'.2e'))



1.23e+04



Inserting Comma
Separators



If you want the number
to be formatted with comma separators, you can insert a comma into the format
specifier.



>>>
print(format(12345.6789, ',.2f'))



12,345.68



>>> print(format(123456789.456,
',.2f'))



123,456,789.46



Specifying a Minimum
Field Width



The format specifier
can also include a minimum field width, which is the minimum number of spaces
that should be used to display the value.



>>> print('The
number is', format(12345.6789, '12,.2f'))



The number is 12,345.68



The number 12,345.68
uses only 9 spaces on the screen, but it is displayed in a field that is 12
spaces wide. When this is the case, the number is right justified in the field.
If a value is too large to fit in the specified field width, the field is
automatically enlarged to accommodate it.


No comments:

Post a Comment