How to replace all spaces by underscores and remove all the parentheses without using replace() function?
<p>Here is what I did. I know this is definitely wrong. I'd like to know how to make this one work? Thx!</p>

<pre><code>import re
def demicrosoft (fn):

"""Clean up a file name.

Remove all parentheses and replace all spaces by underscores.

Params: fn (string): 
Returns: (string) clean version of fn
"""

fn = re.sub('[()]', '', fn)
for ch in ['']:
    fn = fn.translate(ch, "_")
return fn

print(demicrosoft('Quercus stellata (26).jpg'))
</code></pre>

<p>You can use <a href="https://docs.python.org/3/library/stdtypes.html#str.join" rel="nofollow"><code>join</code></a> combined with a generator that iterates over the characters in your string while handling the logic for filtering out parentheses and replacing spaces with underscores:</p>

<pre><code>PARENS = {'(', ')'}

def demicrosoft(string):
    return ''.join('_' if c == ' ' else c for c in string if c not in PARENS)

print(demicrosoft('Quercus stellata (26).jpg'))  # Quercus_stellata_26.jpg
</code></pre>

python replace removeall