'Can someone explain this singly linked last line of code below? <"Node data: %s>" % self.data

class Node:

    def _init_(self, data, next_node = None):
        self.data = data
        self.next_node = next_node

    def _rept_self(self):
 
        return "<Node data: %s>" % self.data

Can someone please explain to me the last line of code?



Solution 1:[1]

This is an old method of string formatting in Python, which is inspired by C syntax. The %s is a placeholder which represents a string, and the variable placed after the % will be used in the place of the %s.

A more intuitive syntax for this would be using an fstring, where variables can be written in curly brackets if an f is placed before the start of a string:

return f"<Node data: {self.data}>"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cameron Jones