5

I am writing documentations for a python package with a clear_stop_character function, in which users can provide extra stop-chars in a list. In the documentation I have written:

"""
stoplist (list, default empty): Accepts a list of extra stop characters,
            should escape special regex characters (e.g., stoplist=['\\*']).
"""

It is crucial for the users to see the double backslash before the stop-char. However, the help() outcome of the built package shows:

"""
stoplist (list, default empty): Accepts a list of extra stop characters,
            should escape special regex characters (e.g., stoplist=['\*']).
"""

So, it will be misleading for the users.
BTW, I did not find a solution based on the previous questions.
Any ideas?

maaniB
  • 552
  • 7
  • 21

1 Answers1

4

\ in Python is an escape character which tells Python to interpret the character following it literally. This means that \\ tells Python to interpret the second \ literally, thus causing the error where the first backslash is not displayed.

The simplest solution to this problem is to use four backslashes: \\\\. This way, Python sees the first backslash and interprets the second one literally, printing \. Then, the third backslash will tell Python to interpret the fourth one literally like \.

Simply rewrite your code as:

"""
stoplist (list, default empty): Accepts a list of extra stop characters,
            should escape special regex characters (e.g., stoplist=['\\\\*']).
"""
q9i
  • 168
  • 9