Session Ready
Exercise

String representation of objects

There are two special methods in Python that return a string representation of an object. __str__() is called when you use print() or str() on an object, and __repr__() is called when you use repr() on an object, print the object in the console without calling print(), or instead of __str__() if __str__() is not defined.

__str__() is supposed to provide a "user-friendly" output describing an object, and __repr__() should return the expression that, when evaluated, will return the same object, ensuring the reproducibility of your code.

In this exercise, you will continue working with the Employee class from Chapter 2.

Instructions 1/2
undefined XP
  • 1
  • 2

Add the __str__() method to Employee that satisfies the following:

  • If emp is an Employee object with name "Amar Howard" and salary of 40000, then print(emp) outputs
Employee name: Amar Howard
Employee salary: 40000