DEV Community

Cover image for The input method explained!
Manitej ⚡
Manitej ⚡

Posted on • Edited on • Originally published at manitej.hashnode.dev

6 2

The input method explained!

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

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (2)

Collapse
 
titanhero profile image
Lex • Edited

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 ⚡

Thanks for the info man 🙌 will look into them

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay