Print object to string

less than 1 minute read

To convert object to string whenever you print it, add the __str__ method in your class like so:

 class Test:
    def __str__(self):
        return "My Objects array "  + ' '.join(map(str, self.tags))

Input:

self.tags = ['t02', 'tr2', 't52', 'tp6', 'th5', 't37', 'ts2', 'tl5', 'tn3', 't44', 'th4', 'tv2', 'tq1', 'tn6', 'td']

Output:

My Objects array t02 tr2 t52 tp6 th5 t37 ts2 tl5 tn3 t44 th4 tv2 tq1 tn6 td

Leave a comment