Learning

Input & Output Operations

Input & Output Operations

input() Function

python
# Basic usage — ALWAYS returns a string! name = input('Enter your name: ') print(f'Hello, {name}!') # Need a number? Convert it! age = int(input('Enter age: ')) height = float(input('Enter height in meters: ')) # Multiple inputs on one line # User types: John 25 name, age = input('Name and age: ').split() # With specific separator # User types: John,25,Engineer name, age, role = input('Enter (comma-separated): ').split(',') # Map to types directly # User types: 10 20 30 nums = list(map(int, input('Numbers: ').split())) print(sum(nums)) # 60

Critical: input() ALWAYS returns str. If you need any other type, you MUST convert.

f-strings (Python 3.6+) — THE Standard

python
name = 'Alice' age = 30 pi = 3.14159 # Basic interpolation print(f'Name: {name}, Age: {age}') # Expressions inside {} print(f'Next year: {age + 1}') print(f'Double pi: {pi * 2}') # Format specs print(f'Pi: {pi:.2f}') # 3.14 print(f'Pi: {pi:.4f}') # 3.1416 print(f'Pi: {pi:10.2f}') # ' 3.14' (width 10) print(f'Number: {42:05d}') # '00042' (zero-padded) print(f'Hex: {255:x}') # 'ff' print(f'Binary: {10:b}') # '1010' print(f'Percent: {0.85:%}') # '85.000000%' print(f'Percent: {0.85:.1%}') # '85.0%' # Alignment print(f'{"Left":<20}') # 'Left ' print(f'{"Center":^20}') # ' Center ' print(f'{"Right":>20}') # ' Right' # Thousand separators print(f'{1000000:,}') # '1,000,000' print(f'{1000000:_}') # '1_000_000' # Calling methods inside f-strings print(f'{name.upper()}') # 'ALICE' print(f'{"hello world".title()}') # 'Hello World' # Nested f-strings (Python 3.12+) # width = 10 # print(f'{"hi":{width}}') # Debugging with = (Python 3.8+) x = 42 print(f'{x=}') # 'x=42' print(f'{x + 1=}') # 'x + 1=43' print(f'{name=!r}') # "name='Alice'" (repr format)

format() Method

python
# Positional print('{} loves {}'.format('Alice', 'Python')) print('{0} loves {1}, {0} loves {1}'.format('Alice', 'Python')) # Named print('{name} is {age}'.format(name='Alice', age=30)) # With format specs print('{:.2f}'.format(3.14159)) # '3.14' print('{:05d}'.format(42)) # '00042'

% Formatting (Legacy)

python
name = 'Alice' age = 30 print('Name: %s, Age: %d' % (name, age)) print('Pi: %.2f' % 3.14159) print('%05d' % 42) # '00042' # Dictionary style print('%(name)s is %(age)d' % {'name': 'Alice', 'age': 30})

Use f-strings unless maintaining legacy code.

Key Rules
  • •input() ALWAYS returns str — convert explicitly with int(), float(), etc.
  • •Use f-strings for all new code — they're fastest, most readable, and most powerful
  • •f'{x=}' debugging syntax (Python 3.8+) prints both variable name and value
  • •Format specs: {value:width.precision type} — e.g., {pi:10.2f} for width-10 with 2 decimals
  • •For multiple numeric inputs: list(map(int, input().split())) is the standard pattern
  • •Use !r inside f-strings to get repr() output instead of str()
Your Task

Create a simple calculator program: take two numbers and an operator (+, -, *, /) as input. Print the result formatted to 4 decimal places if it's a float, or as an integer if the result is whole. Handle division by zero gracefully. Use f-strings with conditional format specs.

EditorPython · JSX
PreviewUpdates on Run Tests
Loading preview…
Tests
Should use input() to get user data
Should convert input to float
Should check for division by zero
Should use f-string with .4f format spec
Should handle whole number result differently
Should use if/elif for operator selection