Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

python - Python中的单引号与双引号[关闭](Single quotes vs. double quotes in Python [closed])

According to the documentation, they're pretty much interchangeable.

(根据文档,它们几乎可以互换。)

Is there a stylistic reason to use one over the other?

(是否有出于某种风格的原因要在一个之上使用另一个?)

  ask by Readonly translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I like to use double quotes around strings that are used for interpolation or that are natural language messages, and single quotes for small symbol-like strings, but will break the rules if the strings contain quotes, or if I forget.

(我喜欢在用于插值或自然语言消息的字符串周围使用双引号,对于像符号一样小的字符串使用单引号,但是如果字符串包含引号或我忘记了,则会违反规则。)

I use triple double quotes for docstrings and raw string literals for regular expressions even if they aren't needed.

(我对文档字符串使用三重双引号,对正则表达式使用原始字符串文字,即使不需要它们也是如此。)

For example:

(例如:)

LIGHT_MESSAGES = {
    'English': "There are %(number_of_lights)s lights.",
    'Pirate':  "Arr! Thar be %(number_of_lights)s lights."
}

def lights_message(language, number_of_lights):
    """Return a language-appropriate string reporting the light count."""
    return LIGHT_MESSAGES[language] % locals()

def is_pirate(message):
    """Return True if the given message sounds piratical."""
    return re.search(r"(?i)(arr|avast|yohoho)!", message) is not None

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...