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 \\>']

The output that I want 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

2

That is because you are printing an array, not a string. If you print a string then this apply the escape character.

but a example of how do it would be:

...
print(*treee)
# print(*treee, sep=",") # if you want custom separator
hiral2
  • 194
  • 1
  • 5
0

When you perform print(treee), what you are seeing is an escaped representation of the backslash in each of the elements in the list.

If you do this instead:

for a_tree in treee:
    print(a_tree)

you will see the single backslash as expected.

MurrayW
  • 393
  • 2
  • 10