Section 1/6The input() Function
The input() Function
The input() function always returns user input as a string. There are two ways to convert to numbers:
Handling User Input & Type Conversion
Interactive programs often need to accept user input. This lesson teaches how to safely convert text input to numbers and use them in calculations.
The input() Function
The input() function always returns user input as a string. There are two ways to convert to numbers:
Method 1: Convert after input
text_input = input("Enter number: ")num = int(text_input)
Method 2: Convert during input
num = int(input("Enter number: "))In these examples:
- Both methods achieve the same result
- Method 2 is more concise for immediate conversion
- Method 1 is better if you need the original string