Home Tutorials Python OOP Concepts _str_ Method
_str_ Method

_str_ Method


28. __str__ Method

The __str__() method returns a readable string representation of an object. It improves how the object appears when printed.

Syntax

def __str__(self):
    return "string"

Example

class Student:
    def __init__(self, name):
        self.name = name
        
    def __str__(self):
        return "Student: " + self.name

s = Student("Anu")
print(s)

Output

Student: Anu
Example

🏋️ Test Yourself With Exercises

Take our quiz on _str_ Method to test your knowledge.

Browse Quizzes »