代码之家  ›  专栏  ›  技术社区  ›  davidmytton

Python中的“string”和“string”有什么区别吗?[副本]

  •  56
  • davidmytton  · 技术社区  · 16 年前

    在PHP中,用“双引号”括起来的字符串将被解析为要替换的变量,而用“单引号”括起来的字符串则不会。在Python中,这也适用吗?

    9 回复  |  直到 14 年前
        1
  •  119
  •   gnat Nat Poor    8 年前

    No :

    2.4.1. 字符串和字节文本

    …通俗易懂:两种文字类型都可以用匹配的单引号括起来( ' " )。它们也可以包含在三个单引号或双引号(通常称为三引号字符串)的匹配组中。反斜杠( \ )字符用于转义具有特殊含义的字符,例如换行符、反斜杠本身或引号字符。。。

        2
  •  40
  •   Bryan Oakley    11 年前

    Python是为数不多的(?)语言之一,其中的“和”具有相同的功能。我的选择通常取决于其中的内容。如果我要引用一个包含单引号的字符串,我将使用双引号,反之亦然,以减少必须转义字符串中的字符。

    示例:

    "this doesn't require escaping the single quote"
    'she said "quoting is easy in python"'
    

    这在python文档的“字符串文本”页面上有记录:

        3
  •  8
  •   Bruno Gomes    14 年前

    在其他一些语言中,如果使用单引号,则不会解释元字符。以Ruby为例:

    irb(main):001:0> puts "string1\nstring2"
    string1
    string2
    => nil
    irb(main):002:0> puts 'string1\nstring2'
    string1\nstring2
    => nil
    

    在Python中,如果希望按字面理解字符串,可以使用原始字符串(前面有“r”字符的字符串):

    >>> print 'string1\nstring2'
    string1
    string2
    >>> print r'string1\nstring2'
    string1\nstring2
    
        4
  •  5
  •   efotinis    16 年前

    'a "quoted" word'
    "another 'quoted' word"
    

    此外,还有三重引号字符串,它们允许引号字符和换行符都不被替换。

    可以使用命名说明符和内置的locals()替换字符串中的变量:

    name = 'John'
    lastname = 'Smith'
    print 'My name is %(name)s %(lastname)s' % locals()  # prints 'My name is John Smith'
    
        5
  •  4
  •   Thorsten    9 年前

    >>> "text"
    'text'
    
    >>> 'text'
    'text'
    

    这可能会让初学者感到困惑,所以我坚持使用单引号(除非您有不同的编码标准)。

        6
  •  4
  •   kxr    8 年前

    “和”字符串引号之间的区别只是在样式上-除了一个在字符串内容中不需要转义另一个之外。

    PEP8 建议一致的规则, PEP257 建议docstring使用三重双引号。

    相同的本政治公众人物并未就此提出建议。选择规则 坚持下去。当字符串包含单引号或双引号时 一串它提高了可读性。

    对于三引号字符串,始终使用双引号字符

    然而,广泛使用的是自然语言字符串(包括插值)更喜欢双引号的做法——因此,任何可能成为I18N候选的内容都是如此。和技术字符串的单引号:符号、字符、路径、命令行选项、技术正则表达式等。。。

    (例如,在为I18N准备代码时,我运行一个半自动的正则表达式来快速转换双引号字符串,以便使用。 gettext )

        7
  •  1
  •   Vasil    16 年前

    有3种方法可以在python中创建字符串: “字符串” """ 一串 它们都产生相同的结果。

        8
  •  0
  •   Steve Moyer    16 年前

    Python没有区别,在生成XML时,您可以真正利用它。正确的XML语法要求在属性值周围使用双引号,在许多语言(如Java)中,这会迫使您在创建如下字符串时对其进行转义:

    String HtmlInJava = "<body bgcolor=\"Pink\">"
    

    html_in_python = '<body bgcolor="Pink">'
    

    multiline_python_string = """
    This is a multi-line Python string which contains line breaks in the 
    resulting string variable, so this string has a '\n' after the word
    'resulting' and the first word 'word'."""
    
        9
  •  -3
  •   gseattle    10 年前

    否则,在下面的代码中,Python处理双引号字符串不会花费额外的4.5%的时间:

    import time
    
    time_single = 0
    time_double = 0
    
    for i in range(10000000):
        # String Using Single Quotes
        time1 = time.time()
        str_single1 = 'Somewhere over the rainbow dreams come true'
        str_single2 = str_single1
        time2 = time.time()
        time_elapsed = time2 - time1
        time_single += time_elapsed
    
        # String Using Double Quotes 
        time3 = time.time()
        str_double1 = "Somewhere over the rainbow dreams come true"
        str_double2 = str_double1
        time4 = time.time()
        time_elapsed = time4 - time3
        time_double += time_elapsed
    
    print 'Time using single quotes: ' + str(time_single)
    print 'Time using double quotes: ' + str(time_double)
    

    输出:

    >python_quotes_test.py
    Time using single quotes: 13.9079978466
    Time using double quotes: 14.5360121727
    

    所以,如果您想要快速、干净、令人尊敬的代码,而您似乎知道自己的东西,那么只要可行,就对字符串使用单引号。跳过shift键也会消耗更少的能量。