DEV Community

loading...
Cover image for The input method explained!

The input method explained!

manitej profile image Manitej ⚑ Originally published at manitej.hashnode.dev ・Updated on ・1 min read

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..")
Enter fullscreen mode Exit fullscreen mode

Reading one variable

Example

age = input("Enter your age")
Enter fullscreen mode Exit fullscreen mode

It works similar to the below code in C++

int age;
court<<"Enter your age";
cin>>age;
Enter fullscreen mode Exit fullscreen mode

Reading two variables

You can also read multiple variables at the same time.

Example

age, weight = input("Enter your age & weight").split()
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Discussion

pic
Editor guide
Collapse
titanhero profile image
Lex

Cool man I don't knew about the split() maybe I'm lost in this but I think that the input() method is deprecated in python2 because it let you to send python expressions, I was reading that it works like a eval so in python2 should be use raw_input() and in python3 raw_input() was become in input(), anyway very cool infoπŸ˜πŸ‘βœŒοΈ

Collapse
manitej profile image
Manitej ⚑ Author

Thanks for the info man πŸ™Œ will look into them