Practical Alignment Examples
def format_financial_report(transactions):
print(f"{'Date':<12}{'Description':<25}{'Amount':>10}")
print("-" * 47)
for date, desc, amount in transactions:
print(f"{date:<12}{desc:<25}{amount:>10.2f}")
transactions = [
('2023-06-01', 'LabEx Subscription', 49.99),
('2023-06-15', 'Cloud Services', 129.50),
('2023-06-30', 'Software License', 199.00)
]
format_financial_report(transactions)
def log_system_status(components):
print(f"{'Component':<15}{'Status':<10}{'Performance':>12}")
print("-" * 37)
for name, status, performance in components:
print(f"{name:<15}{status:<10}{performance:>12.2f}%")
system_components = [
('CPU', 'Active', 65.5),
('Memory', 'Normal', 42.3),
('Disk', 'Healthy', 78.9)
]
log_system_status(system_components)
3. Scientific Data Presentation
def display_experimental_results(experiments):
print(f"{'Experiment':<15}{'Result':<10}{'Deviation':>12}")
print("-" * 37)
for name, result, deviation in experiments:
print(f"{name:<15}{result:<10}{deviation:>12.4f}")
lab_experiments = [
('LabEx Test A', 0.9523, 0.0123),
('LabEx Test B', 1.2345, 0.0456),
('LabEx Test C', 0.7890, 0.0234)
]
display_experimental_results(lab_experiments)
Alignment Strategies
flowchart TD
A[Alignment Strategies] --> B[Fixed Width Formatting]
A --> C[Dynamic Column Sizing]
A --> D[Numeric Precision Control]
A --> E[Contextual Formatting]
Common Alignment Patterns
Pattern |
Use Case |
Key Technique |
Left Align |
Text Descriptions |
{:<width} |
Right Align |
Numeric Values |
{:>width} |
Center Align |
Titles/Headers |
{:^width} |
Precision Control |
Scientific Data |
{:.4f} |
Advanced Alignment Techniques
def create_complex_report(data):
## Multi-column dynamic alignment
print(f"{'Name':<15}{'Department':<20}{'Performance':>12}{'Bonus':>10}")
print("-" * 57)
for entry in data:
print(f"{entry['name']:<15}{entry['dept']:<20}{entry['performance']:>12.2f}{entry['bonus']:>10.2f}")
employee_data = [
{'name': 'Alice Johnson', 'dept': 'Research', 'performance': 92.5, 'bonus': 5000},
{'name': 'Bob Smith', 'dept': 'Engineering', 'performance': 88.3, 'bonus': 4500},
{'name': 'LabEx Team', 'dept': 'Product', 'performance': 95.7, 'bonus': 6000}
]
create_complex_report(employee_data)
Key Takeaways
- Alignment is context-dependent
- Use appropriate formatting for different data types
- Consider readability and professional presentation
- Leverage Python's flexible formatting options