Frozen Blade Frozen Blade - 8 months ago 24
Python Question

Place backslash between words in string

I Want to convert com to newcom

com = R.E.M. - Losing My Religion.mp3

newcom = R.E.M.\ -\ Losing\ My\ Religion.mp3

I am doing this because Ubuntu terminal needs backslashes to specify spaces in paths.

This is just a string manipulation, what do I need to do?

newcom = com.replace(' ', '\\ ')

You need to replace a space with an (escaped) backslash and another space - that's exactly what Python's replace method is for.

Alternatively, an ubuntu terminal is fine with directories in quotes, e.g.

cd "hello world"

is just as valid as your solution, which would give

cd hello\ world

And perhaps cleaner to the user, and more accepting of other characters that might need to be escaped.