printing backslash

micklee74 at hotmail.com micklee74 at hotmail.com
Wed Jun 7 13:09:12 EDT 2006


Tim Chase wrote:

> > i want to print something like this
> >
> > |\|
> >
> > first i tried it as string
> >
> > a = "|\|"
> >
> > it prints ok
> >
> > but when i put it to a list
> >
> > a = ["|\|"]
> >
> > it gives me '|\\|' .there are 2 back slashes...i only want one.. how
> > can i properly escape it?
> > I have tried [r"|\|"] , [r'\\'] but they do not work...
>
> You omit how you're printing matters.
>
>  >>> s1 = '|\|'
>  >>> s2 = r'|\|'
>  >>> s3 = '|\\|'
>  >>> print repr(s1), '->', s1
> '|\\|' -> |\|
>  >>> print repr(s2), '->', s2
> '|\\|' -> |\|
>  >>> print repr(s3), '->', s3
> '|\\|' -> |\|
>
> There's a difference between the repr() of a string (which
> escapes items that need to be escaped) and printing items.  All
> three *print* the item as you request it.  All three represent
> the item with the proper backslashes.
>
> The preferred form of putting backslashes in a string is the s2
> or s3 form, as the s1 form can have some "unpredictable"(*) results:
>
> "\|" happens not to be a recognized escape sequence
> "\t" is, so you get things like
> 	>>> s = '\t\|'
> 	>>> s
> 	'\t\\|'
> -tkc
>
> (*) "unpredictable" defined as, "predictable, if you happen to
> have memorized the exact set of characters that do or don't need
> to beescaped"

thanks, i got  it




More information about the Python-list mailing list