[solved]Escape characters python


Hello everyone,

I have a question, i want to add music to my playlist after it finishes download, which i do using:

import os
number='name of the song'
os.system('mpc add ' + number)

There is a small issue, the following characters need to be escaped:


surprisingly, [] do not need to be escaped. and by escaping i mean inserting a backslash in from of it, so & should become \&, now i could make a whole of regex expressions:

import re
number=re.sub(r'\s+','\\ ',number) # will insert backslash for spaces
number=re.sub(r'([()])', r'\\\1',number) # will insert backslash for spaces

which is the best way to achieve this? A regex expression? Not that good with regex yet, any help would be appreciated, thanks guys :slightly_smiling:

This is outside of codecademy, i run python 3.5.1 on arch linux, mpc is a program to control mpd (music player deamon)

If you think there is a better way then regex, that is good too :slightly_smiling:


It seem i can do this in bash:

mpc add "name_of_song"

then it ignores any special characters, now if i could do this in python.... Would be brilliant, anyone an idea?


Seems i found the solution:

os.system('mpc add ' + '"' + number + '"')