How to center 2 fields combined?

I’m trying to print each tree in a random forest, so want to use something like

for index, tree in enumerate(rf.trees):
    print('{:*^80}{}'.format('tree',index))
    tree.print_tree()

However, the output turns out like ***tree***0 rather than ***tree0***. How to combine variables into string before centering? Is there another method besides str.format?

I used someone’s solution to print a binary tree. You can use the code below or
see the full code at github.

display is the main method. And, display_aux is the helper method.

    def display(self):
        lines, _, _, _ = self.display_aux(self.root)
        for line in lines:
            print(line)

    def display_aux(self, node):
        """Returns list of strings, width, height, and horizontal coordinate of the root."""
        # No child.
        if node.right is None and node.left is None:
            line = '%s' % node.value
            width = len(line)
            height = 1
            middle = width // 2
            return [line], width, height, middle

        # Only left child.
        if node.right is None:
            lines, n, p, x = self.display_aux(node.left)
            s = '%s' % node.value
            u = len(s)
            first_line = (x + 1) * ' ' + (n - x - 1) * '_' + s
            second_line = x * ' ' + '/' + (n - x - 1 + u) * ' '
            shifted_lines = [line + u * ' ' for line in lines]
            return [first_line, second_line] + shifted_lines, n + u, p + 2, n + u // 2

        # Only right child.
        if node.left is None:
            lines, n, p, x = self.display_aux(node.right)
            s = '%s' % node.value
            u = len(s)
            first_line = s + x * '_' + (n - x) * ' '
            second_line = (u + x) * ' ' + '\\' + (n - x - 1) * ' '
            shifted_lines = [u * ' ' + line for line in lines]
            return [first_line, second_line] + shifted_lines, n + u, p + 2, u // 2

        # Two children.
        left, n, p, x = self.display_aux(node.left)
        right, m, q, y = self.display_aux(node.right)
        s = '%s' % node.value
        u = len(s)
        first_line = (x + 1) * ' ' + (n - x - 1) * '_' + s + y * '_' + (m - y) * ' '
        second_line = x * ' ' + '/' + (n - x - 1 + u + y) * ' ' + '\\' + (m - y - 1) * ' '
        if p < q:
            left += [n * ' '] * (q - p)
        elif q < p:
            right += [m * ' '] * (p - q)
        zipped_lines = zip(left, right)
        lines = [first_line, second_line] + [a + u * ' ' + b for a, b in zipped_lines]
        return lines, n + m + u, max(p, q) + 2, n + u // 2

It print something like this.

___6 
 /    \
 2__  8
/   \  
1  _5  
  /    
  3    
   \   
   4   

The following values are inserted into tree: [6, 2, 8, 1, 5, 3, 4]
Tree in ascending order: 1 2 3 4 5 6 8
Tree in descending order: 8 6 5 4 3 2 1
Removing 2: 1 3 4 5 6 8

__6 
 /   \
 3_  8
/  \  
1  5  
  /   
  4

You have two {} place holder and two parameters, 'tree' and index.

However the first {} is {:*^80}. So, the tree gets formatted first before index. It becomes {"tree":*^80} followed by {str(index)}. The output will be ***tree***0.

If you want ***tree0*** as the output, all you need to is concat tree and index into one string and remove the {}. We have

print('{:*^80}'.format('tree' + str(index)))
1 Like