Python prints two backslash instead of one [duplicate]

拜拜、爱过 提交于 2020-01-25 11:57:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!