हिंदी

Explain __str__ with an example.

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: 92
shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 3: Classes in Python - EXERCISE [पृष्ठ ६२]

APPEARS IN

सीबीएसई Computer Science with Python [English] Class 12
अध्याय 3 Classes in Python
EXERCISE | Q 10. | पृष्ठ ६२
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×