though generally simple to usei should mention three usage notes regarding these methods here firstkeep in mind that __str__ and __repr__ must both return stringsother result types are not converted and raise errorsso be sure to run them through to-string converter ( str or %if needed seconddepending on container' string-conversion logicthe user-friendly display of __str__ might only apply when objects appear at the top level of print operationobjects nested in larger objects might still print with their __repr__ or its default the following illustrates both of these pointsclass printerdef __init__(selfval)self val val def __str__(self)return str(self valobjs [printer( )printer( )for in objsprint(xused for instance itself convert to string result __str__ run when instance printed but not when instance is in list print(objs[objs [to ensure that custom display is run in all contexts regardless of the containercode __repr__not __str__the former is run in all cases if the latter doesn' applyincluding nested appearancesclass printerdef __init__(selfval)self val val def __repr__(self)return str(self val__repr__ used by print if no __str__ __repr__ used if echoed or nested objs [printer( )printer( )for in objsprint(xno __str__runs __repr__ print(objs[ objs [ runs __repr__not ___str__ thirdand perhaps most subtlethe display methods also have the potential to trigger infinite recursion loops in rare contexts--because some objectsdisplays include displays of other objectsit' not impossible that display may trigger display of an object being displayedand thus loop this is rare and obscure enough to skip herebut watch operator overloading