Python Input & Output
Learn input() to read user data and print() with sep, end, and f-string formatting.
Getting Input from the User
input() pauses your program, shows a prompt, waits for the user to type something and
press Enter, then returns what they typed as a string. This last point is crucial:
even if the user types 42, you get the string "42", not the integer 42.
Always convert with int() or float() before doing arithmetic.
name = input("What is your name? ") print(f"Hello, {name}!") # Must convert to int for maths age = int(input("Your age? ")) print(f"In 10 years you will be {age + 10}") height = float(input("Height in metres? ")) print(f"Height: {height}m")
What is your name? Alice Hello, Alice! Your age? 25 In 10 years you will be 35 Height in metres? 1.72 Height: 1.72m
Customising print()
print() has two useful keyword arguments. sep controls what goes
between values (default: a space). end controls what comes after
the last value (default: a newline \n). Setting end="" keeps the cursor on
the same line so the next print continues right after.
print("a", "b", "c") # default: space between print("2024", "01", "15", sep="-") # custom separator print("Loading", end="...") # no newline print("Done!") # continues on same line
a b c 2024-01-15 Loading...Done!
Building an Interactive Program
Combining input(), type conversion, and f-strings, you can build useful programs right
away. Here is a simple BMI calculator — it takes two inputs, computes the result, and displays a
formatted message:
print("=== BMI Calculator ===") weight = float(input("Weight (kg): ")) height = float(input("Height (m): ")) bmi = weight / (height ** 2) print(f"\nBMI: {bmi:.1f}") if bmi < 18.5: print("Underweight") elif bmi < 25: print("Normal weight") else: print("Overweight")
=== BMI Calculator === Weight (kg): 70 Height (m): 1.75 BMI: 22.9 Normal weight
ValueError and crashes. Use try/except (Lesson 18) to handle invalid input gracefully.🧠 Quick Check
What type does input() always return?