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;
Top comments (2)
Cool man I don't knew about the
split()
maybe I'm lost in this but I think that theinput()
method is deprecated in python2 because it let you to send python expressions, I was reading that it works like aeval
so in python2 should be useraw_input()
and in python3raw_input()
was become ininput()
, anyway very cool infoππβοΈThanks for the info man π will look into them