John Karimov
John Karimov

Reputation: 161

Python3 prints two backslash

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 .

Upvotes: 0

Views: 318

Answers (2)

hiral2
hiral2

Reputation: 194

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

Upvotes: 2

MurrayW
MurrayW

Reputation: 393

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.

Upvotes: 0

Related Questions