Table of Contents | |
Understanding Python input()
Function
The input() is a primary function in Python for getting user input from the console. When you call input()
, the program’s execution pauses. The program will wait for the user to type something and press Enter. The text the user enters is then returned by input()
as a string. You can then store this string in a variable for further processing. Python input() is fundamental for creating interactive command-line programs.
Syntax of Python input()
user_input = input(prompt_message)
Explanation
user_input
: Variable where the entered string will be stored.input()
: Built-in function to read input.prompt_message
: Optional string is displayed to guide user.
Example of Python input()
name = input("Enter your name: ")
print("Hello, " + name)
Explanation
name = input("Enter your name: ")
: Prompts the user and stores input inname
.print("Hello, " + name)
: Prints a greeting, combining “Hello, “ with entered name.
Output
Enter your name: User
Hello, User
input()
Parameters
The input() function in Python takes only one optional parameter, a prompt string. This string is displayed on the console to the user. The program will still wait for input if you don’t provide a prompt. It just won’t give the user any explicit instruction. It is good practice to include a clear prompt so that users understand what input you expect.
Syntax
variable = input(optional_prompt_string)
Example
age = input("Please enter your age: ")
print("You entered: " + age)
Explanation
age = input("Please enter your age: ")
: Displays the prompt and reads user input.print("You entered: " + age)
: Displays the entered age back to the user.
Output
Please enter your age: 30
You entered: 30
input()
Return Value
Python input() function always returns a string. Even if the user enters a number, that number is provided to your program as a string. If you plan on doing mathematical calculations, you should convert the string input to a different type, like an integer or float. You’ll likely encounter unexpected results or TypeError if you don’t do this conversion.
Syntax
returned_string = input()
Example
user_input = input("Enter a number: ")
print(type(user_input))
Explanation
user_input = input("Enter a number: ")
: Prompts for a number, but input is string.print(type(user_input))
: Prints the data type of theuser_input
.
Output
Enter a number: 55
<class ‘str’>
Taking Integer Input with Python input()
You’ll need to do an explicit type conversion to get an integer input from the user using Python input(). Since input() always returns a string. You’ll use the int() function to convert that string into an integer. If the user enters text that cannot be converted to an integer, your program will show a ValueError.
Syntax
integer_variable = int(input(prompt_for_integer))
Explanation
integer_variable
: Holds the final, converted integer value.int()
: Converts string from input to integer.input(prompt_for_integer)
: Reads user’s typed string from console.
Example
try:
num = int(input("Enter an integer: "))
print("You entered:", num)
except ValueError:
print("Invalid input. Please enter an integer.")
Explanation
try...except
: Manages potentialValueError
.num = int(input("Enter an integer: "))
: Reads the text, attempts integer conversion.print("You entered:", num)
: If conversion succeeds, this prints the integer.except ValueError
: If conversion fails, execute below line.print("Invalid input. Please enter an integer.")
: If failed, prints message to the console.
Output
Enter an integer: 15
You entered: 15
Taking Float Input with Python input()
Similar to integer input, to accept a floating-point number, you’ll need to use Python input() to get the input as a string and then use the float() function to convert it. This allows users to enter numbers with decimal points. If the user enters text that is not a valid float, your program will show a ValueError.
Syntax
float_variable = float(input(prompt_for_float))
Explanation
float_variable
: Stores the final, converted floating-point number.float()
: Transforms string from input into a float.input(prompt_for_float)
: Accepts user’s input string from command-line.
Example
try:
value = float(input("Enter a floating-point number: "))
print("You entered:", value)
except ValueError:
print("Invalid input. Please enter a valid number.")
Explanation
try...except
: This block handles errors during conversion.value = float(input("Enter a floating-point number: "))
: Gets input, tries float conversion.print("You entered:", value)
: Prints thevalue
if the conversion successful.except ValueError
: If conversion fails, execute below line.print("Invalid input. Please enter a valid number.")
: Prints message to the console if conversion failed.
Output
Enter a floating-point number: 2.718
You entered: 2.718
Conclusion
Python input() function is used to accept input from the user. It takes an optional prompt string as a parameter and always returns a string value. So, if you need an integer or float value, you need to convert it using int() or float(), respectively.