NoviceMe
NoviceMe

Reputation: 3256

Getting substring of python string removing escape character

I have following code:

test = 'abc {\"hello\":\"world\"}'
test1 = test[test.find('{'):]
print(test1)

I am looking to get back: {\"hello\":\"world\"} But above code is removing escape characters, is there a way to keep these escape characters in the substring?

Upvotes: 0

Views: 38

Answers (2)

NoviceMe
NoviceMe

Reputation: 3256

Found the solution, this might help someone:

test = r'abc {\"hello\":\"world\"}'
test1 = test[test.find('{'):]
print(test1)

Upvotes: 1

ChristianOConnor
ChristianOConnor

Reputation: 1182

Add an extra "\" before each "\"

test = 'abc {\\"hello\\":\\"world\\"}'
test1 = test[test.find('{'):]
print(test1)

the output is now {\"hello\":\"world\"}

Upvotes: 0

Related Questions