猿问

在引号内使用引号

当我想print在Python中执行命令并且我需要使用引号时,我不知道如何在不关闭字符串的情况下执行此操作。


例如:


print " "a word that needs quotation marks" "

但是当我尝试做我上面做的事情时,我最终关闭了字符串,我不能把我需要的字放在引号之间。


我怎样才能做到这一点?


MMTTMM
浏览 660回答 3
3回答

缥缈止盈

您可以通过以下三种方式之一完成此操作:1)一起使用单引号和双引号:>>> print '"A word that needs quotation marks"'"A word that needs quotation marks"2)转义字符串中的双引号:>>> print "\"A word that needs quotation marks\"""A word that needs quotation marks" 3)使用三引号字符串:>>> print """ "A word that needs quotation marks" """"A word that needs quotation marks" 

神不在的星期二

你需要逃脱它:>>> print "The boy said \"Hello!\" to the girl"The boy said "Hello!" to the girl>>> print 'Her name\'s Jenny.'Her name's Jenny.有关字符串文字,请参阅python页面。

波斯汪

Python接受“和”作为引号,所以你可以这样做:>>> print '"A word that needs quotation marks"'"A word that needs quotation marks"或者,只是逃避内心的>>> print "\"A word that needs quotation marks\"""A word that needs quotation marks"
随时随地看视频慕课网APP

相关分类

Python
我要回答