title: Decimal To Binary Python Getting an Extra Zero In Return String
body: <p>This is for a school project. I need to create a function using recursion to convert an integer to binary string. It must be a str returned, not an int. The base case is n==0, and then 0 would need to be returned. There must be a base case like this, but this is where I think I am getting the extra 0 from (I could be wrong). I am using Python 3.6 with the IDLE and the shell to execute it.</p>
  
  <p>The function works just fine, expect for this additional zero that I need gone. </p>
  
  <p>Here is my function, dtobr:</p>
  
  <pre><code>def dtobr(n):
      """
      (int) -&gt; (str)
      This function has the parameter n, which is a non-negative integer, 
      and it will return the string of 0/1's
      which is the binary representation of n. No side effects. 
      Returns bianry string as mentioned. This is like the function
      dtob (decimal to bianary) but this is using recursion. 
  
      Examples:
  
      &gt;&gt;&gt; dtob(27)
      '11011'
      &gt;&gt;&gt; dtob(0)
      '0'
      &gt;&gt;&gt; dtob(1)
      '1'
      &gt;&gt;&gt; dtob(2)
      '10'
      """
      if n == 0:
          return str(0)
      return dtobr(n // 2) + str(n % 2)
  </code></pre>
  
  <p>This came from the function I already wrote which converted it just fine, but without recursion. For reference, I will include this code as well, but this is not what I need for this project, and there are no errors with this:</p>
  
  <pre><code> def dtob(n):
      """
      (int) -&gt; (str)
  
      This function has the parameter n, which is a non-negative integer, 
      and it will return the string of 0/1's
      which is the binary representation of n. No side effects. 
      Returns bianry string as mentioned.
  
      Examples:
  
      &gt;&gt;&gt; dtob(27)
      '11011'
      &gt;&gt;&gt; dtob(0)
      '0'
      &gt;&gt;&gt; dtob(1)
      '1'
      &gt;&gt;&gt; dtob(2)
      '10'
      """
      string = ""
      if n == 0:
          return str(0)
      while n &gt; 0:
          remainder = n % 2
          string = str(remainder) + string
          n = n // 2
  </code></pre>
  
  <p>Hopefully someone can help me get ride of that additional left hand zero. Thanks!</p>