Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Use a Property to Format Display Output

Learn how to use the @property decorator to expose a computed, read-only attribute that formats instance data for display.

Python practice15 minMethods & PropertiesIntermediateLast updated April 7, 2026

Problem statement

Create a Contact class that stores first_name and last_name. Implement a read-only property called display_name that: - Strips surrounding whitespace from each name part. - Ignores empty parts (so if first or last is empty, it doesn't produce extra spaces). - Title-cases each name part (use Python's title-casing behavior). - Joins the non-empty parts with a single space and returns the resulting string. This property should allow consumers to access a nicely formatted display name as contact.display_name without calling a separate method.

Task

Implement a display_name property that returns a clean, title-cased display string joining first and last name while handling extra whitespace and empty parts.

Examples

Basic formatting

Input

Contact('john','doe').display_name

Output

John Doe

Both parts are trimmed and title-cased, then joined with a space.

Input format

An expression that constructs a Contact and reads the display_name property, e.g. Contact('john','doe').display_name

Output format

A single string with the formatted name, e.g. 'John Doe'.

Constraints

- Do not modify attributes in-place; compute the formatted value when the property is accessed. - Handle None or empty-string gracefully by ignoring those parts. - Use Python string methods; no external libraries.

Samples

Sample 1

Input

Contact(' mary ','ANN ').display_name

Output

Mary Ann

Leading/trailing whitespace removed and both parts title-cased.