FAQ: Learn Python: Classes - String Representation

This community-built FAQ covers the “String Representation” exercise from the lesson “Learn Python: Classes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Learn Python 3

FAQs on the exercise String Representation

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


# You are looking for:
return "Circle with radius {radius}".format(radius=self.radius) 

#but this also works :
return "Circle with {}".format(self.radius) 

:persevere:

2 Likes

3 posts were split to a new topic: What is the use of __repr__?

Hey folks,

I figured out the answer to this exercise but there’s something I don’t get about the output of the code below:

class Circle:
pi = 3.14

def init(self, diameter):
self.radius = diameter / 2

def area(self):
return self.pi * self.radius ** 2

def circumference(self):
return self.pi * 2 * self.radius

def repr(self):
return (‘Circle with radius {radius}’.format(radius=str(self.radius)))

medium_pizza = Circle(12)
teaching_table = Circle(36)
round_room = Circle(11460)

print(medium_pizza)
print(teaching_table)
print(round_room)

I don’t understand why the repr method is the only one that gets called. How come we’re not getting the area, or the circumference? They’re returning something as well… perhaps the answer is obvious but I just don’t see it.

Thanks.

Hello everyone,

In the learn part (left) there is an example where it’s shown that if we just try to print the object argus it will print “<main.Employee object at 0x104e88390>” and this is why we need the repr method.

class Employee():
def init(self, name):
self.name = name

def repr(self):
return self.name

argus = Employee(“Argus Filch”)
print(argus)

My question is what if we just try to print argus.name without repr method? Like this:

class Employee():
def init(self, name):
self.name = name

argus = Employee(“Argus Filch”)
print(argus.name)

#prints “Argus Filch”

Thank you!

Hi folks,

may I ask why there are parentheses after the Employee in class:

class Employee():
  def __init__(self, name):
    self.name = name

while I don’t see them in other class of this lesson? Thank you!

The parentheses aren’t necessary for a class declaration if the class you’re declaring does not inherit from another class. If we want a class to inherit the properties and methods of another class, we can declare it like so: class Employee(Person):. You’ll learn more about the concepts of inheritance and parent and child classes in this exercise.

I’d stick to declaring the class without the parentheses, like so: class Employee:.

Thank you for your reply and it is helpful! Yes when I continued with the following lessons, I realised the cases about the parent class and subclass.

1 Like

Yeah, I’m trying to use the second one since it’s less typing, which is usually better code. But, it won’t accept it sadly, despite giving the correct output.

Hi,

Simple question about repr in this lesson.

Why does it return a “print” without us adding the “print” function to it?

Blockquote
def repr(self):
return “Circle with radius {radius}”.format(radius=self.radius)

Like this is the solution I had success with. Why does the return statement automatically know to print the string?

I also have a sidenote question, but it has nothing to do with this, so feel free to respond or ignore: I recently learned about f-strings, and have understood its a new thing, and basically a simplified syntax for “string”.format if I understand it correctly. Any reason this is not in the lesson, and should I use it if I know about it, or is it better to stick to the “string”.format?