In this post, I'll give you an in-depth use of Python's input()
method.
What is the input used for?
input()
method in Python is used to take input from the user. it works similar to both cout
and cin
in C++
Syntax
variable_name = input("prompt to the user..")
Reading one variable
Example
age = input("Enter your age")
It works similar to the below code in C++
int age;
court<<"Enter your age";
cin>>age;
Reading two variables
You can also read multiple variables at the same time.
Example
age, weight = input("Enter your age & weight").split()
You need to use the split()
method in order to read multiple variables from a single line, The above code works like below,
For example, if you entered 18 49
in the console, the interpreter reads it as a string "18 49"
and the split()
method divides it into 18
and 49
and they are assigned to age
and weight
respectively.
It works similar to the below code in C++
int age, weight;
court<<"Enter your age & weight";
cin>>age>>weight;