escaping - How to Print An Odd Number of Backslashes in Python -
i need print string part of list has 3 backslashes in in python. however, not appearing simple expected.
print ["\\\"] traceback (most recent call last): file "<string>", line 1, in <fragment> eol while scanning string literal: <string>, line 1, pos 13
any string has odd number of backslashes because python escaping quote. tried escaping myself:
print ["\\\\\\"] ['\\\\\\']
which 6 backslashes. not wanted. has stumped few of around water cooler.
'\\\\\\'
string contain 3 backslashes. can see there 3 characters in string applying list
it:
in [166]: list('\\\\\\') out[166]: ['\\', '\\', '\\']
'\\'*3
work:
in [167]: list('\\'*3) out[167]: ['\\', '\\', '\\']
or since
in [169]: hex(ord('\\')) out[169]: '0x5c'
you avoid need escape backslash using \x5c
:
in [170]: print('\x5c\x5c\x5c') \\\
Comments
Post a Comment