This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: A escape character is used when a REGEXP is an argument of "strip" string function
Type: behavior Stage: resolved
Components: Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Tito.Bouzout, eric.smith, ezio.melotti, mrabarnett
Priority: normal Keywords:

Created on 2014-04-17 13:54 by Tito.Bouzout, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg216687 - (view) Author: Tito Bouzout (Tito.Bouzout) Date: 2014-04-17 13:54
Hello!

I got a report that the character "\" was removed from a string using the following code

> "\\server\path\to".strip(r'"\'<>') 

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, 

-- 
Tito
msg216688 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2014-04-17 14:09
The argument isn't a regex, it's a raw string literal consisting of the characters " (quote), \ (backslash), ' (apostrophe), < (less than) and > (greater than).
msg216694 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2014-04-17 14:50
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.
msg216944 - (view) Author: Tito Bouzout (Tito.Bouzout) Date: 2014-04-21 13:30
Thanks guys for the information! Is still weird to me that the escape character is used, but well !  Will remember this bug. 
Kind regards,
History
Date User Action Args
2022-04-11 14:58:02adminsetgithub: 65482
2014-04-21 13:30:18Tito.Bouzoutsetmessages: + msg216944
2014-04-17 14:50:33eric.smithsetstatus: open -> closed

components: - Regular Expressions

nosy: + eric.smith
messages: + msg216694
resolution: not a bug
stage: resolved
2014-04-17 14:09:48mrabarnettsetmessages: + msg216688
2014-04-17 13:54:45Tito.Bouzoutcreate