title: How to use __str__ function to return a string representation of a class
body: <p>I have been tasked with creating an inventory manager in python, and one of the required is to define the following function within the class:</p>
  <p><code>__str__</code> - This function returns a string representation of a
  class.</p>
  <p>The class refers to a file (inventory.txt) which has the following format:
  Country,Code,Product,Cost,Quantity</p>
  <p>my code thus far is as follows:</p>
  <pre><code># Creating the class:
  
  class Shoes():
  
      # Constructor:
      def __init__(self,country,code,product,cost,quantity):
          self.country = country
          self.code = code
          self.product = product
          self.cost = cost
          self.quantity = quantity
      
      # Methods:
      def get_cost():
  
          inventory = open('inventory.txt','r')
          inventory_list = inventory.readlines()
  
          code = input(&quot;What is the code of the product:&quot;)
  
          for line in inventory_list:
              split_lines = line.split(&quot;,&quot;)
              if code == split_lines[1]:
                  print(&quot;This product costs R{}&quot;.format(split_lines[3]))
                  inventory.close()
      
      def get_quantity():
          inventory = open('inventory.txt','r')
          inventory_list = inventory.readlines()
  
          code = input(&quot;What is the code of the product:&quot;)
  
          for line in inventory_list:
              split_lines = line.split(&quot;,&quot;)
              if code == split_lines[1]:
                  print(&quot;There are {} units in inventory&quot;.format(split_lines[4]))
                  inventory.close()
      
      def __str__(self):
          pass
  </code></pre>
  <p>I haven't come across the <strong>str</strong> so I really am not sure how it works, and how to use it. Any advice would be greatly appreciated.</p>