title: How to format a phone number in python
body: <p>So, I've got a really poor implementation of a phone number formatter. It should take the instance value for a phone number format it and return it in format of xxx.xxx.xxxx (for now it should only be a US phone number)</p>
  
  <p>The code can be found on this gist: <a href="https://gist.github.com/trtmn/a5a51c3da55ae0b32ac8" rel="nofollow">https://gist.github.com/trtmn/a5a51c3da55ae0b32ac8</a></p>
  
  <pre><code>    phone = models.CharField(max_length=20, blank=True)
  def formatphone(self): #Dear Future Self - I'm so very sorry.
      formattedphone = ""
      for x in self.phone:
          if x == "1":
              formattedphone = formattedphone + x
          if x == "2":
              formattedphone = formattedphone + x
          if x == "3":
              formattedphone = formattedphone + x
          if x == "4":
              formattedphone = formattedphone + x
          if x == "5":
              formattedphone = formattedphone + x
          if x == "6":
              formattedphone = formattedphone + x
          if x == "7":
              formattedphone = formattedphone + x
          if x == "8":
              formattedphone = formattedphone + x
          if x == "9":
              formattedphone = formattedphone + x
          if x == "0":
              formattedphone = formattedphone + x
      if len(formattedphone) == 11:
          formattedphone = formattedphone[1] + formattedphone[2] + formattedphone[3] + "." + formattedphone[4] + formattedphone[5] + formattedphone[6] + "." + formattedphone[7] + formattedphone[8] + formattedphone[9] + formattedphone[10]
      if len(formattedphone) == 10:
          formattedphone = formattedphone[0] + formattedphone[1] + formattedphone[2] + "." + formattedphone[3] + formattedphone[4] + formattedphone[5] + "." + formattedphone[6] + formattedphone[7] + formattedphone[8] + formattedphone[9]
      return formattedphone
  </code></pre>