2

I am trying to feed some text to a special pupose parser. The problem with this parser is that it is sensitive to ()[] characters and in my sentence in the text have quite a lot of these characters. The manual for the parser suggests that all the ()[] get replaced with \( \) \[ \]. So using str.replace i am using to attach \ to all of those charcaters. I use the code below:

a = 'abcdef(1234)' a.replace('(','\(') 

however i get this as my output:

'abcdef\\(1234)' 

What is wrong with my code? can anyone provide me a solution to solve this for these characters?

1

3 Answers 3

3

That's just how escaped characters (like backslashes) are printed in the REPL. The actual value of the string is as you expected.

>>> a = '\(' >>> a '\\(' >>> print(a) \( 
Sign up to request clarification or add additional context in comments.

Comments

3

Nothing is wrong with your code. This is Python's way to tell you that the string contains a literal \, by showing you that the backslash has been escaped as \\.

That way you can tell if you have two characters, a \ followed by (, or just one character, a escaped bracket \(.

You probably expected to see what you see when you do print 'abcdef\\(1234)'. What you want is what you already have.

Comments

1
suggests that all the ()[] get replaced with \( \) \[ \] 

As i understand, parser itself make the replacement, so if you input:

'abcdef(1234)' 

output will be:

'abcdef\(1234\)' 

So you have to parse the output to get your original text with:

output.replace('\(','(').replace('\)',')')...... 

etc...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.