INPUT()

This is a standard method for acquiring user data from the console, and it always returns the data as a string (strstr) type. Regardless of whether a user enters text, a whole number, or a decimal value, input() captures the literal key presses and stores them as a sequence of characters. This characteristic is crucial for developers to remember, as any data intended for mathematical operations—such as integers or floating-point numbers—must be explicitly converted using the int() or float() functions, often nested directly around the input() call (e.g., age = int(input("How old are you? "))). This required conversion ensures the data is correctly interpreted by the Python interpreter, preventing common TypeError issues that arise from trying to perform arithmetic on string values.

W/O DATA TYPE MODIFICATION

# User input (e.g., "Alice") is stored as a string
name = input()
print("Hello, " + name + "!")

# User input (e.g., "30") is stored as a string
age = input()
print("You are " + age + " years old.")

W/ DATA TYPE MODIFICATION

#!/usr/bin/env python3

def main() -> None:
    # Input is immediately converted from string to integer
    num = int(input());
    print(num);

if __name__ == "__main__":
    main();
    
 * OUTPUT:
    76

W/ PROMPT

#!/usr/bin/env python3

def main() -> None:
    inputNumber = int(input("Enter a number: "));
    print("The value entered is: ", inputNumber);

if __name__ == "__main__":
    main();

Last updated