问题
My goal is to print a backslash in Python3. My input is
links22 = ['1',"n","nkf"]
treee = ['<img src={} \>'.format(i) for i in links22]
print(treee)
The output that I get is:
['<img src=1 \\>', '<img src=n \\>', '<img src=nkf \\>']
And when I try:
print("\\")
The output is:
\
I want to figure out why the first output is \ and in the second is .
回答1:
The first \
is escaping the second, since \
is illegal. In the first example \
is interpenetrate as escape to >
print("\\\\")
Will print \\
回答2:
The answer you can find here: https://docs.python.org/3/library/re.html?highlight=comment%20strings
\
Either escapes special characters (permitting you to match characters like '*', '?', and so forth), or signals a special sequence; special sequences are discussed below.
Most of the standard escapes supported by Python string literals are also accepted by the regular expression parser:
\a \b \f \n
\N \r \t \u
\U \v \x \\
来源:https://stackoverflow.com/questions/58639925/python-prints-two-backslash-instead-of-one