Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在引号内使用引号_Python_String_Python 2.7 - Fatal编程技术网

Python 在引号内使用引号

Python 在引号内使用引号,python,string,python-2.7,Python,String,Python 2.7,当我想用Python执行print命令并且需要使用引号时,我不知道如何在不关闭字符串的情况下执行该命令 例如: print " "a word that needs quotation marks" " 但是当我尝试做我上面所做的事情时,我最终关闭了字符串,我无法将我需要的单词放在引号之间 我该怎么做呢?Python接受“和”作为引号,因此您可以这样做: >>> print '"A word that needs quotation marks"' "A word that

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

例如:

print " "a word that needs quotation marks" "
但是当我尝试做我上面所做的事情时,我最终关闭了字符串,我无法将我需要的单词放在引号之间

我该怎么做呢?

Python接受“和”作为引号,因此您可以这样做:

>>> print '"A word that needs quotation marks"'
"A word that needs quotation marks"
或者,只需逃避内心的恐惧

你需要逃避它。(使用Python 3打印函数):


请参阅python页面,以了解更多信息。

您可以通过以下三种方式之一实现此目的:

  • 同时使用单引号和双引号:
  • 转义字符串中的双引号:
  • 使用三重引号字符串:

  • 使用文字转义字符
    \

    print("Here is, \"a quote\"")
    

    字符基本上意味着忽略下一个字符的语义上下文,并按字面意义处理它。

    在Windows上的Python 3.2.2中

    print(""""A word that needs quotation marks" """) 
    

    没关系。我认为这是Python解释器的增强。

    您也可以尝试添加字符串:
    打印“+”+”一个需要引号“+”的单词

    在副本中普遍存在的一种情况是要求在外部进程中使用引号。一个解决方法是不使用shell,这样就不需要一个级别的引用

    os.system("""awk '/foo/ { print "bar" }' %""" % filename)
    
    可以有效地替换为

    subprocess.call(['awk', '/foo/ { print "bar" }', filename])
    
    (这也修复了
    filename
    中的shell元字符需要从shell中转义的错误,而原始代码未能做到这一点;但是如果没有shell,就不需要这样做)

    当然,在绝大多数情况下,您根本不想要或不需要外部流程

    with open(filename) as fh:
        for line in fh:
            if 'foo' in line:
                print("bar")
    

    当您有几个像这样的单词需要连接成一个字符串时,我建议使用
    format
    f-strings
    ,这将显著提高可读性(在我看来)

    举个例子:

    s = "a word that needs quotation marks"
    s2 = "another word"
    
    现在你可以做了

    print('"{}" and "{}"'.format(s, s2))
    
    哪个会打印

    "a word that needs quotation marks" and "another word"
    
    从Python 3.6开始,您可以使用:

    print(f'"{s}" and "{s2}"')
    

    产生相同的输出。

    我很惊讶还没有人提到

    标志
    !r
    repr()
    内置函数1的缩写。它用于打印对象表示形式
    对象。
    而不是
    对象。

    不过有一个有趣的副作用:

    请注意,引号的不同组合是如何处理的,以使其适合Python对象2的有效字符串表示形式


    如果有人不知道,请纠正我


    2问题的原始示例
    “word”
    在Python中不是有效的表示形式

    这在空闲的Python 3.8.2中对我有效

    print('''"A word with quotation marks"''')
    

    三重单引号似乎允许您将双引号作为字符串的一部分包含在内。

    对双引号进行转义是可行的,但是您添加的额外空格现在是字符串的一部分,可以通过去掉空格轻松修复。与三重引号字符串类似,额外的空格成为字符串的一部分,我不知道如何解决这一问题,因为将内部“放在三重引号旁边-”会导致语法错误。您可以使用单三重引号避免语法错误:“”“一个词”“”,但是现在事情变得越来越愚蠢了。如果你有几个像这样的词,你想连接起来,我会用。还有一个特定于Python 3.6的答案,非常可读。4)
    print(“\'一个需要引号的单词\”)
    s = "a word that needs quotation marks"
    s2 = "another word"
    
    print('"{}" and "{}"'.format(s, s2))
    
    "a word that needs quotation marks" and "another word"
    
    print(f'"{s}" and "{s2}"')
    
    >>> print('{!r}'.format('a word that needs quotation marks'))
    'a word that needs quotation marks'
    
    >>> print("{!r} \t {!r} \t {!r} \t {!r}".format("Buzz'", 'Buzz"', "Buzz", 'Buzz'))
    "Buzz'"      'Buzz"'     'Buzz'      'Buzz'
    
    print('''"A word with quotation marks"''')