How to use input and print function in python
To accept the value from user in python
In python, to get input from user interactively, you can use
built-in function input (). This function always returns a value of
string type.
Syntax:
Variable-name=input(“msg”)
For example:
name=input (“Enter your name”)
age=input (“Enter your age”)
Screenshot:
Here, we used input () function to input two values name and
age. But while displaying both values name and age, input () treated as string
values.
If you tried to perform arithmetic calculation on the value
of age, it raised an error. Suppose you tried to add 1 to age, it generates an
error i.e. called as Type Error. Because python cannot add an integer to
a string. We can check the variable type with the help of type ()
function.
String values cannot be used for arithmetic or other numeric
operations. For arithmetic operations, you need to have values of numeric types
like integer or float. Integer for numeric values and float for floating type
values/fractional values.
Python provides us two functions int () and float
() to be used with input () function to convert the string values in
to integer or float type.
Syntax:
Variable-name= int (input (“msg”))
For example:
Age=int (input (“Enter your age”))
Screenshot:
Note: While inputting integer values or floating type values
using int () function or with input (), the entered number or value must be
compatible with integer type.
The entered values like (17.5, Seventeen) with
integer type, it is not an int compatible value.
Similarly, the value
entered (56.5,56.5.8,45 percent) are not float compatible value. It
raises an error.
Please note that the values like 87,87. Or .87 are all float
convertible type; python will automatically convert them to float type values.
Output through print () statement | How to display the output in python 3
The print () function of python 3.x is a way to send output on
the screen.
For example:
print (“learn python”)
Or
print (“sum of 3 and 4 is:”,3+4)
Or
print (“Square root of”, a,” is:”, a*a)
Screenshot:
Print () function without any value or name or expression
prints a blank line. It automatically converts the numeric value in to the
string type and prints it.
'sep' argument with print () function in python
It inserts spaces
between string automatically because the default value of sep argument is space
(‘‘). The sep argument specifies the separator character. The print ()
automatically adds the sep character between the strings.
For example:
print (“I”,” want”, “to”,” learn”, “python”)
Output:
I want to learn python
You can change the value of separator character with 'sep' argument of print ().
For example:
print (“I”,” want”,” to”,” learn”,” python.”, sep= ‘…’)
Output:
I... want... to... learn... python.
'end' argument with print () function in python
By default, the print () function takes value for end
argument as ‘/n’ – the newline character, when you have not specified any end
argument with it. So, the print () statement appended a newline at the end of given
output it printed.
For example:
print (“I want to learn python.”)
print (“I love programming.”)
Output:
I want to learn python.
I love programming.
You can change an 'end' argument by giving another value.
For example:
print (“I want to learn python.”, end=’$’)
print (“I love programming.”)
Output:
I want to learn python. $I love programming.
Screenshot:
No comments:
Post a Comment