- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What does the str() function do in Python Object Oriented Programming?
The __str__ method
__str__ is a special method, like __init__, that returns a 'informal' string representation of an object. It is useful in debugging.
Consider the following code that uses the __str__ method
class Time: def __str__(self): return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)
When we print an object, Python invokes the str method −
>>> time = Time(7, 36) >>> print time 07:36:00
Advertisements