0

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 .

John Karimov
  • 151
  • 1
  • 1
  • 9

2 Answers2

1

The first \ is escaping the second, since \ is illegal. In the first example \ is interpenetrate as escape to >

print("\\\\")

Will print \\

Guy
  • 46,488
  • 10
  • 44
  • 88
0

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      \\
otluk
  • 317
  • 1
  • 4
  • 16