input() is a Python built-in function for interacting with a user. It displays an optional prompt, waits until the user presses Enter, and returns everything entered as a string. Leave a space at the end of the prompt so the user's response is easy to read. section 11.2
Store a response
user_place = input("Where do you live? ") text = user_place.capitalize() + "!" print(text) The first line saves the response in user_place. The next line changes its capitalization and adds punctuation; print() displays the result. section 11.2
Every response from input() is text, even when the user types a number. Convert it before performing numerical operations: int() creates an integer and float() creates a decimal number. section 11.3
Calculate with responses
num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) print(num1, "*", num2, "=", num1 * num2) Each input is converted to a float before multiplication. section 11.2
Remember these points when using input():
- The program pauses at input() until the user presses Enter. section 11.3
- Assign the result to a variable when later code needs the response. section 11.2
- Convert numeric responses explicitly before calculations. section 11.3
Listing 11.4. Storing the user input
user_place = input("Where do you live? ") #1
text = user_place.capitalize() + "!" #2
print(text) #3
print("I hear it's nice there!") #3
See a complete interaction that stores a response, modifies it, and prints messages. section 11.2






