Feb-08-2025, 02:58 AM
Hi, the function def __str__(self) while I put inside the class is giving indent error. But if put outside doesn't give output.
plz advice.
plz advice.
Attached Files
| def __str__(self) gives error |
| Feb-08-2025, 02:58 AM Hi, the function def __str__(self) while I put inside the class is giving indent error. But if put outside doesn't give output. plz advice. Attached Files Feb-08-2025, 07:29 AM Use code tags The def __str__ method most be inside the Student class. Then you just print(s1) to get output from __str__class Student: def __init__(self,name, roll_no): self.name = name self.roll_no = roll_no def __str__(self): return f"{self.name} has roll number {self.roll_no}." s1 = Student("Arjun", 1) print(s1) s1.name = "Dhananjay" print(s1) |
| |
| Possibly Related Threads… | |||||
| Thread | Author | Replies | Views | Last Post | |
| TypeError: __str__ returned non-string (type tuple) | Anldra12 | 1 | 9,558 | Apr-13-2021, 07:50 AM Last Post: Anldra12 | |
| TypeError: __str__ returned non-string error | jolinchewjb | 5 | 12,924 | Jan-24-2019, 07:54 AM Last Post: jolinchewjb | |
| Magic method __str__ | dan789 | 16 | 13,122 | Dec-23-2018, 03:59 PM Last Post: ichabod801 | |