New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A escape character is used when a REGEXP is an argument of "strip" string function #65482
Comments
Hello! I got a report that the character "\" was removed from a string using the following code
At first insight, looks like a bug, because I don't expect the use of the escape character at all. Then I noticed, that our mistake there is that the "strip" argument should be a "string" not a REGEXP. Kinda weird to read, and I've no idea if this is expected behaviour in Python, as I'm relatively very new. So just informing, Kind regards, -- |
The argument isn't a regex, it's a raw string literal consisting of the characters " (quote), \ (backslash), ' (apostrophe), < (less than) and > (greater than). |
In addition, you probably want "\\server\path\to" to be a raw string, too. That way, the backslashes are not given special meaning. Notice the difference in output between these two: >>> "\\server\path\to".strip(r'"\'<>')
'server\\path\to'
>>> r"\\server\path\to".strip(r'"\'<>')
'server\\path\\to' In the first one, '\t' is being treated as a tab character, in the second one you see a backslash followed by a 't'. My rule of thumb is: any time you have a string with a filename containing backslashes, you want it to be a raw string. |
Thanks guys for the information! Is still weird to me that the escape character is used, but well ! Will remember this bug. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: