Split on backslash merges split elements

Viewed 55

I try to split a string on a backslash. However, the split method doesn't return 2 elements but just 1 without the backslash:

'0.023750\0.023746'.split("\\")
> ["0.023750.023746"]
1 Answers

split method doesn't work, because "\0" is special symbol as "\n" or "\r" that means NULL. So that why your string is interpreted incorrectly. Solution is: write before your string symbol 'r'. It will say to interpreter to ignore special symbols.

r'0.023750\0.023746'.split("\\")
> ['0.023750', '0.023746']
Related