![]() |
VOOZH | about |
Programs need to collect information from users, such as their name, age or a number. This is done using the input() function, which:
Unlike some languages that rely on dialog boxes, Python keeps it simple by taking input directly from the console. This program asks the user for a value, stores it as a string and then prints it back.
Output
Enter your value: 123
123
When you use input() in a program:
Example: This program checks the data type of user input to confirm that both number and name are stored as strings.
Output
Enter number: 45
45
Enter name: John
John
Type of number: <class 'str'>
Type of name: <class 'str'>
Often, you need numerical input (like age, marks, or salary). Since input() returns a string, you must convert it using int() or float().
Note: If the entered value cannot be converted (for example, int("abc")), Python raises a ValueError.
Example 1: Integer Input
This program converts user input into an integer.
Output
Enter a number: 25
25 is of type <class 'int'>
Example 2: Floating-Point Input
This program converts user input into a float (decimal number).
Output
Enter a decimal number: 19.75
19.75 is of type <class 'float'>
Sometimes, a program needs to accept more than one value at once. In Python, this can be done by entering values separated by spaces and then splitting them into separate variables.
Example: This program takes two numbers from the user in one input line, splits them and prints them separately.
Output
Enter two numbers separated by space: 10 20
First number: 10
Second number: 20