input: Write a function to perform various matrix operations like addition, subtraction, multiplication, and transpose using a class in Python. The class should be initialized with the dimensions of the matrix and a list of its elements. The class should also have methods to perform the mentioned operations, and a __str__ method to print the matrix.
  
  
  class Matrix: 
      def __init__(self, m, n, lst): 
          self.m = m             
          self.n = n             
          self.mat = [] 
    
          c = 0
          for i in range(self.m): 
              a =[] 
              for j in range(self.n): 
                      a.append(lst[c]) 
                      c += 1
              self.mat.append(a) 
    
      def __str__(self): 
          output = ""
          for i in self.mat: 
              for j in i: 
                  output += str(j) + " "
              output += '\n'
          return output 
    
      def add(self, mat2): 
          for i in range(self.m): 
              for j in range(self.n): 
                  self.mat[i][j] += mat2.mat[i][j] 
    
      def sub(self, mat2): 
          for i in range(self.m): 
              for j in range(self.n): 
                  self.mat[i][j] -= mat2.mat[i][j]
      
      def mul(self, mat2): 
          result = [] 
          for i in range(self.m): 
              a =[] 
              for j in range(mat2.n): 
                  s = 0
                  for k in range(self.n): 
                      s += self.mat[i][k] * mat2.mat[k][j] 
                  a.append(s) 
              result.append(a) 
          self.mat = result 
          self.m = len(result) 
          self.n = len(result[0]) 
    
      def transpose(self):
          result = [[self.mat[j][i] for j in range(len(self.mat))] for i in range(len(self.mat[0]))]
          self.mat = result 
          self.m = len(result) 
          self.n = len(result[0])
  
  # Test cases
  matrix1 = Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
  matrix2 = Matrix(3, 3, [9, 8, 7, 6, 5, 4, 3, 2, 1])
  
  # Addition test
  matrix1.add(matrix2)
  assert str(matrix1) == "10 10 10 \n10 10 10 \n10 10 10 \n"
  
  # Subtraction test
  matrix1 = Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
  matrix1.sub(matrix2)
  assert str(matrix1) == "-8 -6 -4 \n-2 0 2 \n4 6 8 \n"
  
  # Multiplication test
  matrix1 = Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
  matrix1.mul(matrix2)
  assert str(matrix1) == "30 24 18 \n84 69 54 \n138 114 90 \n"
  
  # Transpose test
  matrix1 = Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
  matrix1.transpose()
  assert str(matrix1) == "1 4 7 \n2 5 8 \n3 6 9 \n"
  
output: 
  def __init__(self, m, n, lst): 
      self.m = m             
      self.n = n             
      self.mat = [] 
  
      c = 0
      for i in range(self.m): 
          a =[] 
          for j in range(self.n): 
                  a.append(lst[c]) 
                  c += 1
          self.mat.append(a) 
  
  def __str__(self): 
      output = ""
      for i in self.mat: 
          for j in i: 
              output += str(j) + " "
          output += '\n'
      return output 
  
  def add(self, mat2): 
      for i in range(self.m): 
          for j in range(self.n): 
              self.mat[i][j] += mat2.mat[i][j] 
  
  def sub(self, mat2): 
      for i in range(self.m): 
          for j in range(self.n): 
              self.mat[i][j] -= mat2.mat[i][j]
      
  def mul(self, mat2): 
      result = [] 
      for i in range(self.m): 
          a =[] 
          for j in range(mat2.n): 
              s = 0
              for k in range(self.n): 
                  s += self.mat[i][k] * mat2.mat[k][j] 
              a.append(s) 
          result.append(a) 
      self.mat = result 
      self.m = len(result) 
      self.n = len(result[0]) 
  
  def transpose(self):
      result = [[self.mat[j][i] for j in range(len(self.mat))] for i in range(len(self.mat[0]))]
      self.mat = result 
      self.m = len(result) 
      self.n = len(result[0])
  