Back to course

Getting User Input: The `input()` Function

Python Programming: The 0 to Hero Bootcamp

Interacting with the User

The input() function pauses the program execution and waits for the user to type something and press Enter.

Basic Input

python user_name = input("Please enter your name: ") print(f"Welcome, {user_name}!")

Crucial Detail: Input Returns Strings

The input() function always returns the user's input as a string, even if they type numbers.

If you want to perform calculations, you must explicitly cast the input to an int or float.

python

Scenario 1: Incorrect (treating input as string)

num1_str = input("Enter first number: ") # User types 10 num2_str = input("Enter second number: ") # User types 5

print(num1_str + num2_str) # Output: 105 (string concatenation)

Scenario 2: Correct (casting the input)

num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: "))

print(f"The sum is: {num1 + num2}") # Output: 15

Note: If the user enters non-numeric text when you try to cast it as an int() or float(), your program will crash (we will learn how to handle this gracefully in the error handling section).