Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - INPUT & OUTPUT


Input and Output are preferably the most important statements in a programming language.


So far, we have seen the Output command of Python(i.e. 'print(...)').


Example :



print("Hello world") 


And we are yet to look at the Input statement.


So, let us look at the Input statement. Then we will be exploring a few more ways to use the Output statement.


Python Input


So far, we have used variables to store values. But the variables were loaded with values.


Example :



x = 9
print("The value of x is : ",x)


Output :



  The value of x is : 9

But, what if you don't want the variable 'x' to contain a fixed value like '9'. Rather you want the user to enter any value of their choice.


And this is where the Input statements comes to picture.


Just like the Output statement is called 'print()'. Same way the Input statement is called as 'input()'.


Note : Just note that Python 2.7 used 'raw_input()' as Input statement. Now, with Python 3.6,the Input statement is just 'input()'.

Let us understand 'input()' statement with the below example.


Example :



x = input("Enter any number of your choice :")
print("The number is : ",x)


Output :



  Enter any number of your choice : 5
  The number is : 5

Now, if you see the Output. The first line says,


Enter any number of your choice : 5

So, what happened is, the below String is Printed,


Enter any number of your choice :

And the string is stuck until user types any number.


Python Input&Output

In the above case, I have typed '5'.


Enter any number of your choice : 5

Python Input&Output

And we got the output,


The number is : 5

Next, let us look at Python Output.


Python Output


A simple example of 'print(...)' statement would be,


Example :



x = 5
print("The value of x is : ",x)


Output :



  The value of x is : 5

Also, we can print multiple values separated by ',' using the 'print(...)' statement.


Example :



print(5,6,9)


Output :



  5  6  9

Keyword Argument


With the 'print(...)' statement, Python supports something called as 'Keyword Argument'.


We will be looking at two types of 'Keyword Argument'. i.e. 'sep' and 'end'.


Let us see them below :


  1. 'sep' Keyword Argument

    The abbreviation of 'sep' Keyword Argument is 'Separator'. Let us make it lear with the below example.

    Example :



    print('John','Paul','Ram',sep='->') 
    


    Output :



      John->Paul->Ram


    So, we wanted to print the names 'John','Paul' and 'Ram'. And separate the names with '->'.

    And to separate the names, we have used the 'sep' Keyword Argument(i.e. 'sep='->'').

    print('John','Paul','Ram',sep='->')


    Let us take another example, where we wanted to print three numbers using a separator '&'.

    Example :



    print(5, 3, 8, sep='&')  
    


    Output :



       5&3&8


    And we got the above output.
  2. 'end' Keyword Argument

    The 'end' Keyword Argument, as the name suggests is used to place a character at the end of the print(...) statement.

    Example :



    print(5, 3, 8, end='!') 
    


    Output :



      5  3  8!


    If you see the output, '!' is printed at the end of the output.

Formatting Output using '%'


With the 'print(...)' statement, Python supports something called as 'Output Formatting'.


Let us dive into an example to understand 'Output Formatting'.


Example :



print('%s has a Son who studies in class %d' %('Paul', 4)) 

Output :



  Paul has a Son who studies in class 4

So, inside the 'print(...)' statement, there are two parts.


print('%s has a Son who studies in class %d' %('Paul', 4))

The left part has the actual String to be printed, with two, so called placeholders %s and %d.


'%s has a Son who studies in class %d'

And the right side has the values to be placed in the above placeholder.


%('Paul', 4)

Now, if you look at the output,


Paul has a Son who studies in class 4

You can see that '%s' is replaced with 'Paul' and '%d' is replaced with '4'.


Similarly, we can use other placeholders for all types of data.


Below is the List :



Type Description
d, i, u Decimal Number
x, X Hexadecimal Number
o Octal Number
f, F Floating Point Number
e, E Exponential Number
g, G Floating point/Exponential
c Single character
s, r, a String

Formatting Output using 'format()' method


Formatting Output can also be done using the 'format()' method.


Let us rewrite the above example using the 'format()' method.


Example :



print('{} has a Son who studies in class {}'.format('Paul', 4)) 

Output :



  Paul has a Son who studies in class 4

Just like the above example, this 'print(...)' statement also has two parts.


print('{} has a Son who studies in class {}'.format('Paul', 4))

The left part has the actual String to be printed, with braces '{ }' as placeholders.


'{} has a Son who studies in class {}'

And the right side has the 'format()' method with values to be placed in the above placeholder i.e. '{}'.


.format('Paul', 4)

Now, if you look at the output,


Paul has a Son who studies in class 4

You can see that both the placeholders (i.e. '{}') is replaced with 'Paul' and '4'.


Just note that in this case the values, 'Paul' and '4' are placed inside the '.format()' method.


We can also specify the order in which the values could be printed.


Let us explain with the below example.


Example :



print('{1} has a Son who studies in class {0}'.format(4, 'Paul')) 

Output :



  Paul has a Son who studies in class 4

Just like the above example, this 'print(...)' statement also has two parts.


print('{1} has a Son who studies in class {0}'.format(4, 'Paul'))

But this time, the left part has the order inside braces '{ }' as placeholders.


'{1} has a Son who studies in class {0}'

And the 'format()' method has the values '4' at first and 'Paul' at the second place.


.format(4, 'Paul')

Now, if you look at the output,


Paul has a Son who studies in class 4

'Paul' is printed to the first place. Because we have specified the order.


'{1} has a Son who studies in class {0}'