User Input in Python
User input allows your Python program to interact with the user. You can prompt the user to enter data during program execution using the built-in input()
function.
By default, input()
returns data as a string — you must explicitly cast it to other types as needed.
Basic Syntax
input_basic.py
name = input("Enter your name: ")
print("Hello,", name)
output.txt
Enter your name: Alice
Hello, Alice
Explanation:
input(prompt)
displays the prompt message.- The function returns the user input as a string.
Example 1: Input as Integer
input_int.py
age = int(input("Enter your age: "))
print("You are", age, "years old.")
output.txt
Enter your age: 25
You are 25 years old.
Explanation:
- Input is cast to integer using
int()
. - If the input is not a valid number, an error occurs.
Example 2: Input as Float
input_float.py
temperature = float(input("Enter temperature in Celsius: "))
print("Temperature is", temperature, "°C")
output.txt
Enter temperature in Celsius: 36.6
Temperature is 36.6 °C
Explanation:
- Input is cast to float using
float()
for decimal values.
Example 3: Input as Boolean
input_bool.py
value = input("Enter True or False: ")
bool_value = value.lower() == "true"
print("Boolean value:", bool_value)
output.txt
Enter True or False: True
Boolean value: True
Explanation:
- Since
input()
returns a string, we convert the string to lowercase and compare to"true"
to simulate boolean input.
Example 4: Input as List (from String)
input_list.py
numbers = input("Enter numbers separated by spaces: ")
num_list = numbers.split()
print("List of numbers:", num_list)
output.txt
Enter numbers separated by spaces: 1 2 3 4 5
List of numbers: ['1', '2', '3', '4', '5']
Explanation:
- The
split()
method splits the input string into a list of strings. - You can convert each to an integer using list comprehension.
Example 5: Input as List of Integers
input_list_int.py
numbers = input("Enter numbers separated by spaces: ")
num_list = [int(num) for num in numbers.split()]
print("List of integers:", num_list)
output.txt
Enter numbers separated by spaces: 10 20 30
List of integers: [10, 20, 30]
Explanation:
- List comprehension is used to convert each string to an integer.
Example 6: Input as Tuple
input_tuple.py
values = input("Enter values separated by commas: ")
tup = tuple(values.split(","))
print("Tuple:", tup)
output.txt
Enter values separated by commas: a,b,c
Tuple: ('a', 'b', 'c')
Explanation:
- The input string is split on commas and converted into a tuple.
Example 7: Input as Set
input_set.py
values = input("Enter unique values separated by spaces: ")
value_set = set(values.split())
print("Set:", value_set)
output.txt
Enter unique values separated by spaces: apple banana apple orange
Set: {'banana', 'orange', 'apple'}
Explanation:
- The input string is split and converted to a set to remove duplicates.
Summary
- The
input()
function always returns a string. - Use type casting (
int()
,float()
,bool()
,list()
, etc.) to convert input to desired data type. - For collections (list, tuple, set), use
split()
and comprehensions to process the input.