User Input & Output
Objective
Read user input and produce formatted output using print() and f-strings.
Tools & Technologies
input()print()format()f-stringssys.stdout
Key Commands
name = input('Name: ')print(f'Hello, {name}!')print('{:.2f}'.format(3.14159))print('value:', x, sep='\t', end='\n')Architecture Overview
flowchart LR
USER[User types] -->|keyboard| INPUT[input() function]
INPUT -->|returns string| VAR[Variable]
VAR -->|process| PROC[Your code]
PROC -->|result| PRINT[print() function]
PRINT -->|stdout| SCREEN[Terminal output]
style INPUT fill:#1a1a2e,stroke:#00d4ff,color:#e0e0e0
style PRINT fill:#1a1a2e,stroke:#00ff88,color:#e0e0e0
Step-by-Step Process
01
Input and Type Conversion
input() always returns a string — convert as needed.
name = input('Enter your name: ') # string
age = int(input('Enter age: ')) # convert to int
temp = float(input('Temperature: '))
# Safe conversion with error handling
try:
age = int(input('Age: '))
except ValueError:
print('Please enter a number')
02
Formatted Output
Control how values are displayed.
# f-strings (most readable)
pi = 3.14159
print(f'Pi is approximately {pi:.2f}')
print(f'{'Left':<10} | {'Right':>10}')
# format() method
print('{} + {} = {}'.format(1, 2, 3))
print('{0:.2f} {1}'.format(3.14159, 'radians'))
# print parameters
print('a', 'b', 'c', sep='-') # a-b-c
print('Loading...', end='') # no newline
print(' Done!')
Challenges & Solutions
- input() returns a string even when user types a number — convert before arithmetic
- print() adds newline by default — use end='' to suppress
Key Takeaways
- f-strings are 30-40% faster than % formatting in Python 3.6+
- input() with no prompt is confusing — always provide a descriptive prompt string