Home > other >  Remove Leading and Trailing Single Quote when String Formatting in Python
Remove Leading and Trailing Single Quote when String Formatting in Python

Time:07-03

I'm trying to learn python so I decided to implement a Singly Linked List but I stumbled upon a weird problem.

Screenshot 1

As you could see from the screenshot above, the output when I'm trying to search for the index of a node has a leading and trailing single quote.

class Node:
  next_node = None

  def __init__(self, data):
    self.data = data

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

class LinkedList:
  def __init__(self):
    self.head = None
    self.tail = None

  def is_empty(self):
    return self.head == None

  def size(self):
    current = self.head
    count = 0

    while current:
      count  = 1
      current = current.next_node
    return count

  def append(self, data):
    new_node = Node(data)

    if self.is_empty():
      self.head = new_node
    else:
      self.tail.next_node = new_node

    self.tail = new_node

  def search_node(self, key):
    current = self.head
    position = 0

    while current:
      if current.data == key:
        # This returns with a leading and trailing single quote
        return "<Node index: %s>" % position
      else:
        current = current.next_node
        position  = 1
    return None

  def search_index(self, index):
    if index == 0:
      return self.head
    elif index == self.size() - 1:
      return self.tail
    else:
      current = self.head
      position = 0

    while position < index:
      current = current.next_node
      position  = 1
    return current

  def __repr__(self):
    nodes = []
    current = self.head

    if self.is_empty():
      return "<Empty>"

    if self.size() == 1:
      return "<Head: Tail: %s>" % current.data

    while current:
      if current is self.head:
        nodes.append("<Head: %s>" % current.data)
      elif current.next_node is None:
        nodes.append("<Tail: %s>" % current.data)
      else:
        nodes.append("<%s>" % current.data)

      current = current.next_node
    return "-> ".join(nodes)

What's causing this behavior? I saw some articles about the repr function causing it but I'm not really sure.

CodePudding user response:

I made some notes in your code that will hopefully help with identifying why your outputs are not consistent.

class LinkedList:
  def __init__(self):
    self.head = None  < - node or None
    self.tail = None  < - node or None

  def search_node(self, key):  # <- function return will be str or None
    current = self.head
    position = 0

    while current:
      if current.data == key:
        return "<Node index: %s>" % position  # <-- Returning a string
      else:
        current = current.next_node
        position  = 1
    return None              # <--- Returning None

  def search_index(self, index):   # <- function returns node or None
    if index == 0:
      return self.head  # <- returing node or None
    elif index == self.size() - 1:
      return self.tail  # <- returning node or None
    else:
      current = self.head
      position = 0

    while position < index:
      current = current.next_node
      position  = 1
    return current      # <- returning node or None

The __repr__(str) of a string will always have the quotes surrounding it.

  • Related