title: How to escape parentheses python
body: <p>I am the newbie in programming. The goal of this exercise is to convert a string to a new string where each character in the new string is &quot;(&quot; if that character appears only once in the original string, or &quot;)&quot; if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate.</p>
  <p>But when the code meet ) -- closing parentheses it generate wrong output. As I figured out, the problem is with regular expressions, but I didn't get how to fix code.</p>
  <pre><code>from collections import Counter
  
  def duplicate_encode(word):
      counter = Counter(word.lower())
      counter2 = dict.copy(counter)
  
      print(counter2)
  
      for k,v in counter2.items():
          if counter2[k]==1:
              counter2[k]='('
          else:
              counter2[k]=')'
    
      for key in counter2.keys():
          word = str(word.lower()).replace(key, str(counter2[key])) 
      
      return word
  </code></pre>
  <p>For example:</p>
  <p><code>duplicate_encode('yy! R)!QvdG')</code> should return <code>)))((()((((</code> but I got <code>(((((((((((</code>.</p>