Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Validate and Sanitize Data in a Property Setter

Practice using @property setters to validate and sanitize attributes like username, email, and age.

Python practice16 minMethods & PropertiesIntermediateLast updated April 7, 2026

Problem statement

Implement a User class with three attributes: username, email, and age. Use @property decorators to control access: - username: sanitize by stripping leading/trailing whitespace and capitalizing the first letter (e.g. ' alice ' -> 'Alice'). If the sanitized username is empty, raise ValueError. - email: sanitize by trimming whitespace and lowercasing. Validate with a basic check: it must contain exactly one '@' and at least one '.' in the domain portion (after the '@'). If invalid, raise ValueError. - age: accept integer-like values (ints or digit strings) and store as an int. If conversion is not possible or the result is negative, raise ValueError. Provide a summary() helper that returns a string like "Alice <alice@example.com> (30)".

Task

Implement property getters and setters that sanitize input (strip/normalize) and validate values, converting types when appropriate.

Examples

Create and read sanitized email

Input

User(' Alice ', 'ALICE@EXAMPLE.COM', 30).email

Output

alice@example.com

Email is trimmed and lowercased.

Input format

Constructor parameters: username (str), email (str), age (int or numeric string).

Output format

Accessors return sanitized values: username (str), email (str), age (int).

Constraints

- username must be a non-empty string after trimming - email must include one '@' and at least one '.' after '@' - age must convert to a non-negative integer - On invalid input, raise ValueError

Samples

Sample 1

Input

User(' bob ', 'bob@site.com', '0').username

Output

Bob

Username trimmed and capitalized; age string converted to int 0.