In Python input() method is used to take input from the user through the keyboard and convert into a String. The type of the returned object always will be <class ‘str’>.
Here it allows you to prompt the user for input and read the text they enter, which can be stored in a variable for further processing or manipulation in your program.
In Simple Words; The input() method reads a line from the keyboard (the user input), converts the input into a string.
Example
>>> name=input(“Enter Customer Name :”)
Enter Customer Name : Sanduni
Here the user input text “Sanduni” will be assigned to the python variable “name”
The phrase in the input function is not compulsory.
>>> name=input()
Nimal
>>>print(name)
Nimal
If you need to convert the entered string into int, the int() function should be used.
>> marks=int(input(“Enter Student Marks :”))
>> temp=float(input(“Enter Temparature :”))
Taking multiple inputs from user for Python input() method
Taking multiple inputs from the user means that from the keyboard in Python typically refers to the process of receiving more than one data from the user in a single input operation. This is often done by provide a series of values separated by a specific delimiter(a separator), such as a space, comma, or newline.
Example of how you can take multiple inputs from the user in Python:
Here you should use split() method to break the given input by the specified delimiter.
>>> m1, m2=input(“Enter Subject Marks:”).split(“,”)
Enter Subject Marks:75,85
>>> print(m1)
75
>>> print(m2)
85
See above; it’s difficult to give number of variables to assign multiple inputs as in the above example (m1, m2)
so that the best way is to define and create a list in Python to take multiple inputs from the user at once.
>>> x = [int(x) for x in input(“Enter Student Marks: “).split()]
Enter Student Marks: 50 60 70 80 90
>>> print(x)
[50, 60, 70, 80, 90]
Python output methods also explained in the next lesson