class Person:
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
def __str__(self):
return f'Person({self.first_name},{self.last_name},{self.age})'
Code language: Python (python)
def __str__(self):
return self.name
The __str__ method in Python represents the class objects as a string – it can be used for classes.
This method is called when print() or str() function is invoked on an object. This method must return the String object.