1. Learn
  2. /
  3. Courses
  4. /
  5. Object-Oriented Programming in Python

Connected

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 the Employee class that returns a string containing the employee's name and salary. The method should return a string in the following format:

Employee name: Amar Howard
Employee salary: 40000