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

Python-Module1_3[1]

The document provides an overview of input/output operations in Python, focusing on the use of the input() and print() functions for user interaction. It explains how to read numbers and strings, handle data types, and format output using various methods including string interpolation and formatted string literals (f-strings). Additionally, it covers extensions of the print statement and formatting techniques to enhance output presentation.

Uploaded by

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

Python-Module1_3[1]

The document provides an overview of input/output operations in Python, focusing on the use of the input() and print() functions for user interaction. It explains how to read numbers and strings, handle data types, and format output using various methods including string interpolation and formatted string literals (f-strings). Additionally, it covers extensions of the print statement and formatting techniques to enhance output presentation.

Uploaded by

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

Python Programming for Data Science [BDS306D]

6. Simple input/output print statements


Usually the values are assigned to variables using the assignment operator. In real world, a
program needs to communicate with the external world through I/O devices such as keyboard
and console. The program should be able to interact with the user. Input function input() and
output function print() are used by python as I/O statements.

6.1 Reading numbers from Keyboard: we can read the input from console using input()
function. The syntax of the statement for a number is

and for a string is

Ex:

Ex: to illustrate error

The above code results in error, because, Python reads numbers also as a string. inpit() produces only
string. Even if the user inputs an integer or float number, it is read as a strng. This problem can be
avoided by converting the string to an integer.

The problem of knowing the number if integer or floating point number and for the truncation
of floating point number, the eval() function is used.
Ex:

In this case, irrespective of what user types, int, float, string or complex, Python accepts the
number and determines the type of value entered by the user.
Python Programming for Data Science [BDS306D]

6.2 print() statement:


The print statement in python converts all python objects into a string and writes it on general
I/O stream. The syntax is

the argument can be integer, float, string or any other data type.
Ex:

Single or double quotes are used for printing more than one argument by separating them with
a comma. Numbers and variables can be printed directly.
Escape sequence should be used to print special characters.
Ex.

Print statement can accept multiple arguments.


Ex:

An empty print() statement creates new line \n character.

6.3 Extensions of print statement:

Python print() function has 2 attributes - sep and end.


Python Programming for Data Science [BDS306D]

Ex for sep:

Ex for end:

In the above example the end flag ( attribute) is used to create a comma between operands an
and b.

7. Formatting print statement


All variables and constants need to be converted into strings type for printing, this is called
formatting.
7.1 Built in methods:
These sequences start with a back slash character. Some of the unprintable characters can be
printed using escape sequences, as shown below.
Python Programming for Data Science [BDS306D]

7.2 String Interpolation:


It is a way of combining a string with constants or variables at specific locations to create a
user-specified output. This replacement part is called a placeholder, some are as shown below.

% is an operator that replaces the placeholders. Python supports % character to format the
strings by replacing the placeholders with constants and variables, the syntax is

Values are the arguments supplied to the format string so that the output is as per the user’s
specification.
Ex:

The below lines of python code shows how an integer and string can be combined in the string
interpolation method.

7.3 String Formatting : This is the second way of formatting. Here there will be a format string
and arguments. The syntax is

The placeholders are in the form of curly braces, {}, and are called named replacements or
placeholders.
Python Programming for Data Science [BDS306D]

This formatting helps create emails, form letters or web pages.

Here there is no placeholder, hence, the string is printed as it is. That is to say, the print()
function searches for placeholders, in this case. If none are available, it prints the strings
without any formatting.
Python allows the replacement fields and placeholders to put variables and concatenate strings
for printing.
Ex:

Ex: using built-in formats for printing

Alternately, the arguments for format can be index or positional order. The arguments are
passed as tuples and indexing starts from 0. The arguments would be substituted by removing
the placeholder, as shown in example below.

‘Name’ replaces the argument{0} and the word ‘is’ replaces the argument{1} wherever that
numbered placeholders are present. The arguments can be out of order or can be repeated,
arguments are swapped in the above example.
As shown below, the arguments occur multiple times.
Python Programming for Data Science [BDS306D]

The format can provide field width and precision. It can be specified as :n, where ‘n’ is the
number of characters in the field. If the data is having less than the character width, the extra
spaces are added to the right.

7.4 Formatted String Literals (F-string)


F-strings are also known as literal string interpolation. F-strings are string literals that start
with f or F at the beginning of the string. {} are the placeholders that hold the expressions that
are replaced by the values. The best way to format the string is with an f-string because they
are faster, more readable, more concise and less error prone. One of the primary benefits is that
it simplifies string interpolation. It also insists on a simple way to stuff a python expression
into a string literal for formatting. The use of f-strings is shown below.
Python Programming for Data Science [BDS306D]

You might also like