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(...)').
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.
So far, we have used variables to store values. But the variables were loaded with values.
x = 9 print("The value of x is : ",x)
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()'.
Let us understand 'input()' statement with the below example.
x = input("Enter any number of your choice :") print("The number is : ",x)
Now, if you see the Output. The first line says,
So, what happened is, the below String is Printed,
And the string is stuck until user types any number.
In the above case, I have typed '5'.
And we got the output,
Next, let us look at Python Output.
A simple example of 'print(...)' statement would be,
x = 5 print("The value of x is : ",x)
Also, we can print multiple values separated by ',' using the 'print(...)' statement.
print(5,6,9)
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 :
print('John','Paul','Ram',sep='->')
print(5, 3, 8, sep='&')
print(5, 3, 8, end='!')
With the 'print(...)' statement, Python supports something called as 'Output Formatting'.
Let us dive into an example to understand 'Output Formatting'.
print('%s has a Son who studies in class %d' %('Paul', 4))
So, inside the 'print(...)' statement, there are two parts.
The left part has the actual String to be printed, with two, so called placeholders %s and %d.
And the right side has the values to be placed in the above placeholder.
Now, if you look at the output,
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 can also be done using the 'format()' method.
Let us rewrite the above example using the 'format()' method.
print('{} has a Son who studies in class {}'.format('Paul', 4))
Just like the above example, this 'print(...)' statement also has two parts.
The left part has the actual String to be printed, with braces '{ }' as placeholders.
And the right side has the 'format()' method with values to be placed in the above placeholder i.e. '{}'.
Now, if you look at the output,
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.
print('{1} has a Son who studies in class {0}'.format(4, 'Paul'))
Just like the above example, this 'print(...)' statement also has two parts.
But this time, the left part has the order inside braces '{ }' as placeholders.
And the 'format()' method has the values '4' at first and 'Paul' at the second place.
Now, if you look at the output,
'Paul' is printed to the first place. Because we have specified the order.