When I want to do a print
command in Python and I need to use quotation marks, I don't know how to do it without closing the string.
当我想在Python中执行打印命令并且需要使用引号时,我不知道如何在不关闭字符串的情况下执行。
For instance:
例如:
print " "a word that needs quotation marks" "
But when I try to do what I did above, I end up closing the string and I can't put the word I need between quotation marks.
但是当我尝试做上面所做的事情时,我最终关闭了字符串,我不能在引号之间加上我需要的单词。
How can I do that?
我怎么做呢?
8 个解决方案
#1
136
You could do this in one of three ways:
你可以用以下三种方法之一:
1) Use single and double quotes together:
1)使用单引号和双引号:
>>> print '"A word that needs quotation marks"'
"A word that needs quotation marks"
2) Escape the double quotes within the string:
2)转义字符串中的双引号:
>>> print "\"A word that needs quotation marks\""
"A word that needs quotation marks"
3) Use triple-quoted strings:
3)使用三引号字符串:
>>> print """ "A word that needs quotation marks" """
"A word that needs quotation marks"
#2
7
You need to escape it:
你需要摆脱它:
>>> print "The boy said \"Hello!\" to the girl"
The boy said "Hello!" to the girl
>>> print 'Her name\'s Jenny.'
Her name's Jenny.
See the python page for string literals.
请参阅python页面中的字符串文字。
#3
3
Python accepts both " and ' as quote marks, so you could do this as:
Python接受“和”作为引号,因此您可以这样做:
>>> print '"A word that needs quotation marks"'
"A word that needs quotation marks"
Alternatively, just escape the inner "s
或者,只是逃离内心的“s”。
>>> print "\"A word that needs quotation marks\""
"A word that needs quotation marks"
#4
3
Use the literal escape character \
使用文字转义字符\
print("Here is, \"a quote\"")
The character basically means ignore the semantic context of my next charcter, and deal with it in its literal sense.
这个字符基本上是指忽略我下一个角色的语义上下文,并按字面意思处理它。
#5
1
in Python 3.2.2 on Windows,
在Python 3.2.2中,
print(""""A word that needs quotation marks" """)
is ok. I think it is the enhancement of Python interpretor.
就可以了。我认为这是Python解释器的改进。
#6
1
One case which is prevalent in duplicates is the requirement to use quotes for external processes. A workaround for that is to not use a shell, which removes the requirement for one level of quoting.
在副本中普遍存在的一种情况是对外部进程使用引号的要求。解决这个问题的方法是不使用shell,这就消除了对一级引用的要求。
os.system("""awk '/foo/ { print "bar" }' %""" % filename)
can usefully be replaced with
可以被有用的替代吗
subprocess.call(['awk', '/foo/ { print "bar" }', filename])
(which also fixes the bug that shell metacharacters in filename
would need to be escaped from the shell, which the original code failed to do; but without a shell, no need for that).
(它还修复了在文件名中shell元字符需要从shell中转义的错误,这是原始代码没有做到的;但没有壳,就没有必要这么做)。
Of course, in the vast majority of cases, you don't want or need an external process at all.
当然,在大多数情况下,您根本不需要或根本不需要外部过程。
with open(filename) as fh:
for line in fh:
if 'foo' in line:
print("bar")
#7
0
You could also try string addition: print " "+'"'+'a word that needs quotation marks'+'"'
您还可以尝试字符串添加:打印“”+“”+“+”一个需要引号的词“+”
#8
0
When you have several words like this which you want to concatenate in a string, I recommend using format
or f-strings
which increase readability dramatically (in my opinion).
当你有几个像这样的词想要连接到一个字符串时,我推荐使用格式或f-string,它们可以显著提高可读性(在我看来)。
To give an example:
举一个例子:
s = "a word that needs quotation marks"
s2 = "another word"
Now you can do
现在你能做的
print('"{}" and "{}"'.format(s, s2))
which will print
这将打印
"a word that needs quotation marks" and "another word"
As of Python 3.6 you can use:
在Python 3.6中,您可以使用:
print(f'"{s}" and "{s2}"')
yielding the same output.
产生相同的输出。
#1
136
You could do this in one of three ways:
你可以用以下三种方法之一:
1) Use single and double quotes together:
1)使用单引号和双引号:
>>> print '"A word that needs quotation marks"'
"A word that needs quotation marks"
2) Escape the double quotes within the string:
2)转义字符串中的双引号:
>>> print "\"A word that needs quotation marks\""
"A word that needs quotation marks"
3) Use triple-quoted strings:
3)使用三引号字符串:
>>> print """ "A word that needs quotation marks" """
"A word that needs quotation marks"
#2
7
You need to escape it:
你需要摆脱它:
>>> print "The boy said \"Hello!\" to the girl"
The boy said "Hello!" to the girl
>>> print 'Her name\'s Jenny.'
Her name's Jenny.
See the python page for string literals.
请参阅python页面中的字符串文字。
#3
3
Python accepts both " and ' as quote marks, so you could do this as:
Python接受“和”作为引号,因此您可以这样做:
>>> print '"A word that needs quotation marks"'
"A word that needs quotation marks"
Alternatively, just escape the inner "s
或者,只是逃离内心的“s”。
>>> print "\"A word that needs quotation marks\""
"A word that needs quotation marks"
#4
3
Use the literal escape character \
使用文字转义字符\
print("Here is, \"a quote\"")
The character basically means ignore the semantic context of my next charcter, and deal with it in its literal sense.
这个字符基本上是指忽略我下一个角色的语义上下文,并按字面意思处理它。
#5
1
in Python 3.2.2 on Windows,
在Python 3.2.2中,
print(""""A word that needs quotation marks" """)
is ok. I think it is the enhancement of Python interpretor.
就可以了。我认为这是Python解释器的改进。
#6
1
One case which is prevalent in duplicates is the requirement to use quotes for external processes. A workaround for that is to not use a shell, which removes the requirement for one level of quoting.
在副本中普遍存在的一种情况是对外部进程使用引号的要求。解决这个问题的方法是不使用shell,这就消除了对一级引用的要求。
os.system("""awk '/foo/ { print "bar" }' %""" % filename)
can usefully be replaced with
可以被有用的替代吗
subprocess.call(['awk', '/foo/ { print "bar" }', filename])
(which also fixes the bug that shell metacharacters in filename
would need to be escaped from the shell, which the original code failed to do; but without a shell, no need for that).
(它还修复了在文件名中shell元字符需要从shell中转义的错误,这是原始代码没有做到的;但没有壳,就没有必要这么做)。
Of course, in the vast majority of cases, you don't want or need an external process at all.
当然,在大多数情况下,您根本不需要或根本不需要外部过程。
with open(filename) as fh:
for line in fh:
if 'foo' in line:
print("bar")
#7
0
You could also try string addition: print " "+'"'+'a word that needs quotation marks'+'"'
您还可以尝试字符串添加:打印“”+“”+“+”一个需要引号的词“+”
#8
0
When you have several words like this which you want to concatenate in a string, I recommend using format
or f-strings
which increase readability dramatically (in my opinion).
当你有几个像这样的词想要连接到一个字符串时,我推荐使用格式或f-string,它们可以显著提高可读性(在我看来)。
To give an example:
举一个例子:
s = "a word that needs quotation marks"
s2 = "another word"
Now you can do
现在你能做的
print('"{}" and "{}"'.format(s, s2))
which will print
这将打印
"a word that needs quotation marks" and "another word"
As of Python 3.6 you can use:
在Python 3.6中,您可以使用:
print(f'"{s}" and "{s2}"')
yielding the same output.
产生相同的输出。