How to take input by console in python, explain with example.
How to take input by console in python, explain with example.
Student
The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .
1. What Is User Input?
Example use cases:
2. The
input()FunctionIn Python, you can take user input using the built-in
input()function.Syntax:
The message inside the parentheses (
"Your message here: ") is displayed on the screen.Whatever the user types is captured as a string (text).
Example:
Output:
3. All Input Is a String
Even if the user types a number, Python reads it as a string by default.
Example:
Output:
4. Converting Input to Numbers
If you want to perform calculations, you must convert the string to a number.
Convert to integer (
int)Convert to float (
float)5. Example — Simple Calculator
Output:
6. Using
input()with Multiple ValuesYou can take multiple inputs in one line and split them.
Example:
Output:
7. Prompt Customization and Formatting
You can make your prompt more readable using string format type:
8. Common Mistakes
num = input()thenprint(num + 5)int()eval(input())Summary
name = input("Enter your name: ")age = int(input("Enter age: "))price = float(input("Enter price: "))a, b = input().split()