Python字符串嵌套在字符串下

Python字符串嵌套在字符串下,python,string,text,Python,String,Text,当我运行下面的代码行时,我得到以下结果: >>> a = ''' this " is what \"is 'needed' \" Please " ''' ' this " is what "is \'needed\' " Please " ' 我希望输出为: ' this " is what "is 'needed' " Please " ' 问题是“正在被\”取代。 有人能建议一种解决方法吗?您只需要打印字符串: a = ''' this " is what \"is

当我运行下面的代码行时,我得到以下结果:

>>> a = ''' this " is what \"is 'needed' \" Please " '''
' this " is what "is \'needed\' " Please " '
我希望输出为:

' this " is what "is 'needed' " Please " '
问题是
正在被
\”
取代。
有人能建议一种解决方法吗?

您只需要打印字符串:

a = ''' this " is what \"is 'needed' \" Please " '''
print(a)
输出:

this " is what "is 'needed' " Please " 
' this " is what "is 'needed' " Please " '
或者,如果您确实需要外部引号:

print("'" + a + "'")
输出:

this " is what "is 'needed' " Please " 
' this " is what "is 'needed' " Please " '

您正在回显字符串对象,因此将显示
repr()
表示。这是为了调试输出,可以很容易地复制到Python脚本中,因为它仍然是有效的字符串文字语法,将重新创建完全相同的值。您通常希望使用
print()
来编写字符串值本身。有什么原因需要外部的“…”引号作为打印值的一部分吗?我需要外部的“…”,整个东西都传递给一个函数,所以不能打印它。如果你将它传递给一个函数,那么这里没有错,反斜杠不是值的一部分,只是表示的一部分。对,所以你有一个。您错误地诊断了问题,并询问了有关您的诊断的问题,而不是您试图解决的真正问题。听起来你需要使用SQL参数。如果他们想要那些外部单引号呢?基本上我无法打印,这个“a”将被传递到函数中,但是你说:
“我希望输出是:
(等等)。看起来这不是问题。只需将
a
传递到函数中,就完成了。实际上,该示例是一个演示,该“a”被传递到paramiko会话,该会话是传递到linux服务器的sql查询。查询应该只包含“where item='Y'”,但它将包含“where item=\'Y\”