title: How to convert 10 digits with this format XXX-XXX-XXXX to US formal format that looks like (XXX) XXX-XXXX using Python3 Regex sub
body: <blockquote>
  <p>This is my try, it actually put the first and seconds groups of 3 digits between parenthesis while I only need to put the first group only between parenthesis to meet US phone number formal format like (XXX) XXX-XXXX.
  I am asked to do this using re.sub only which means it is a pattern matter and correct syntax which I am missing actually.
  Thank you very much.</p>
  </blockquote>
  <pre><code>import re 
  def convert_phone_number(phone): 
     result = re.sub(r&quot;(\d+-)&quot;, r&quot;(\1)&quot;, phone) # my actual pattern - change only this line
     return result                              
  
  print(convert_phone_number(&quot;My number is 212-345-9999.&quot;)) # output should be: My number is (212) 345-9999.
  # my actual output: My number is (212-)(345-)9999.
  print(convert_phone_number(&quot;Please call 888-555-1234&quot;)) # output should be: Please call (888) 555-1234
  # my actual output: Please call (888-)(555-)1234
  </code></pre>