[Tutor] backslashes

Dave Kuhlman dkuhlman at rexx.com
Sun Jul 8 22:16:14 CEST 2007

On Sun, Jul 08, 2007 at 10:54:51AM -0600, max . wrote:
> hello i am writing a simple password tester that uses a word list and am
> running into some problems when i read the words from a text file they are
> written to the screen with a backslash at the end and i cant seem to find a
> way to get rid of them

In Python, the backslash is a character escape character.  In order
to include a backslash in a string, use a double backslash.  Here
is some example code::

    In [1]: s1 = 'abcd\\efg\\'
    In [2]: s1
    Out[2]: 'abcd\\efg\\'
    In [3]: len(s1)
    Out[3]: 9
    In [4]: s1.replace('\\', '')
    Out[4]: 'abcdefg'

Notice the length of the string (before removing the backslashes). 
Each apparently double backslash actually has a length of 1.


Dave Kuhlman

More information about the Tutor mailing list