Advertisements
Advertisements
प्रश्न
Explain __str__ with an example.
कोड लेखन
स्पष्ट करा
Advertisements
उत्तर
__str__ is a special method that returns the readable string representation of an object. It is called automatically by print(object) or str(object).
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def __str__(self):
return f"Name: {self.name}, Marks: {self.marks}"
s1 = Student("Aman", 92)
print(s1)
Name: Aman, Marks: 92shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
