In a Python program we use the predefined function input()
to read data entered on the standard input stream (keyboard).
Example
name = input () city = input () |
When the interpreter encounters the input()
function, it waits for the user to enter data on the standard input stream (keyboard) until the Enter
key is pressed.
The entered character sequence can be stored in a variable of type str
(string) for later use.
When the Enter
key is read, the program moves to the next instruction.
To display a prompt message, it is passed as an argument to the input()
function.
Example
name = input ( 'Enter your name : ' ) city = input ( 'Enter your city : ' ) |
The type of a data read using the input()
function is str
(string).
To read a numeric data type, we use the predefined functions int()
, float()
to convert the data read using the input()
function.
Example
width = int ( input ( "Enter width : " )) height = int ( input ( "Enter height : " )) print (width * height) |
In a Python program we use the predefined function print()
to write (display) data to the standard output stream (screen.)
Example
print ( 'Hello World!' ) |
The print()
function accepts multiple arguments that are valid expressions.
Example
width = 7 height = 5 print ( 'width : ' , width) print ( 'height : ' , height) print ( 'area : ' , width * height) |
The print()
function emits a newline
character ( \n
) at the end.
Consequently, the message displayed by the next print()
statement appears on the next line of the console.
To display the results of two successive print()
statements on the same line, we tell the print()
function to emit a string as a separator at the end, and we do this by passing it as an argument named end
.
Example
width = 7 height = 5 print ( 'width : ' , width, end = ' ' ) print ( 'height : ' , height, end = ' ' ) print ( 'area : ' , width * height) |
The print()
function displays its arguments separated by a space.
To use another character as a separator, we pass it as an argument named sep
to the print()
function.
Example: arguments separated by a comma.
width = 7 height = 5 area = width * height print (width, height, area, sep = ',' ) |
The str.format()
method is used to format the display with the print()
function.
The curly braces { }
are used to specify the location and order of the arguments passed to the print()
function.
Example
width = 7 height = 5 print ( 'width : {} height : {}' . format (width, height)) |
The f-Strings
format makes it easy to display values and variables.
Example
name = 'apple' family = 'fruit' print (f '{name} is a {family}' ) |
Example: using the format()
method.
i = 255 print ( 'left : {:<8}' . format (i)) print ( 'center : {:^8}' . format (i)) print ( 'right : {:>8}' . format (i)) print ( 'zero : {:08}' . format (i)) print ( 'bin : {:b}' . format (i)) print ( 'oct : {:o}' . format (i)) print ( 'hex : {:x}' . format (i)) |
Executing these instructions displays the following messages:
left : 255 center : 255 right : 255 zero : 00000255 bin : 11111111 oct : 377 hex : ff |
Example: using f-Strings format.
i = 255 print (f 'left : {i:<8}' ) print (f 'center : {i:^8}' ) print (f 'right : {i:>8}' ) print (f 'zero : {i:08}' ) print (f 'bin : {i:b}' ) print (f 'oct : {i:o}' ) print (f 'hex : {i:x}' ) |
Executing these instructions displays the following messages:
left : 255 center : 255 right : 255 zero : 00000255 bin : 11111111 oct : 377 hex : ff |
Example: using the format()
method.
r = 0.1234 print ( 'digit : {:.2}' . format (r)) print ( 'digit : {:.6f}' . format (r)) print ( 'exp : {:.4e}' . format (r)) print ( 'percent : {:.0%}' . format (r)) |
Executing these instructions displays the following messages:
digit : 0.12 digit : 0.123400 exp : 1.2340e-01 percent : 12% |
Example: using f-Strings format.
r = 0.1234 print (f 'digit : {r:.2}' ) print (f 'digit : {r:.6f}' ) print (f 'exp : {r:.4e}' ) print (f 'percent : {r:.0%}' ) |
Executing these instructions displays the following messages:
digit : 0.12 digit : 0.123400 exp : 1.2340e-01 percent : 12% |