0
0
Pythonprogramming~15 mins

Formatting using format() method in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Formatting using format() method
📖 Scenario: You are preparing a simple report card for students. You want to display their names and scores neatly aligned in columns.
🎯 Goal: Build a program that formats and aligns student names and scores using the format() method.
📋 What You'll Learn
Create a dictionary with student names and their scores
Create a variable for the width of the name column
Use a for loop to format each student's name and score using the format() method
Print the formatted output
💡 Why This Matters
🌍 Real World
Formatting text output is useful for creating readable reports, invoices, or tables in console applications.
💼 Career
Many jobs require displaying data clearly. Knowing how to format strings helps in data presentation and user interfaces.
Progress0 / 4 steps
1
Create the student scores dictionary
Create a dictionary called students with these exact entries: 'Alice': 85, 'Bob': 92, 'Charlie': 78
Python
Need a hint?

Use curly braces {} to create the dictionary and separate entries with commas.

2
Set the width for the name column
Create a variable called name_width and set it to 10 to define the width for the name column
Python
Need a hint?

Just assign the number 10 to the variable name_width.

3
Format each student's name and score
Use a for loop with variables name and score to iterate over students.items(). Inside the loop, create a variable line that formats name left-aligned in a field of width name_width and score right-aligned in a field of width 3 using the format() method
Python
Need a hint?

Use {:<{width}} to left-align the name and {:>3} to right-align the score.

4
Print the formatted lines
Inside the for loop, add a print(line) statement to display each formatted student name and score
Python
Need a hint?

Use print(line) inside the loop to show each formatted line.