Single Quotes Vs. Double Quotes in Python

Single quotes vs. double quotes in Python

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

What is the difference, if any, between using single quote and double quote in a python dictionary?

The differences is that when you have a string like:

''a''

It will throw a syntax-error because it contains two quotes side by side

so you need to do double quotes like:

"'a'"

And same thing doing the opposite:

""a""

Will throw an error.

'"a"'

won't throw an error

(on stack-overflow the displayed code is already doesn't look right)

For Python language, What is the difference between single and double quotation marks

The uses of quotation marks and double quotation marks are the same, but you can use them in different cases; I'll give some examples as follows;

# Use of quotation mark ''
print('This is a "python code"')

# Use of double quotation mark ""
print("This is a 'python code'")

# More specific case (triple quotation mark)
print('''"This is a 'python code'"''')

If quotation mark is in a text to print, you should use double quotation mark.
If double quotation mark is in a text to print, you should use quotation mark.
If both of them is in a text to print, you should use triple quotation mark.

I'd be very happy if you let me know your opinion.

Single quoted string vs. double quoted string

It doesn't matter if you use ' or " around the string to mark it as string literal. But you can't use that character inside the string literal without escaping it using a \ in front of it - otherwise Python interprets it as the end of the string.

For example " inside a " delimited string literal need to be escaped as well:

a = "And he said: \"What a nice day\"."

Understanding difference between Double Quote and Single Quote with __repr__()

There is no semantic difference between ' and ". You can use ' if the string contains " and vice versa, and Python will do the same. If the string contains both, you have to escape some of them (or use triple quotes, """ or '''). (If both ' and " are possible, Python and many programmers seem to prefer ', though.)

>>> x = "string with ' quote"
>>> y = 'string with " quote'
>>> z = "string with ' and \" quote"
>>> x
"string with ' quote"
>>> y
'string with " quote'
>>> z
'string with \' and " quote'

About print, str and repr: print will print the given string with no additional quotes, while str will create a string from the given object (in this case, the string itself) and repr creates a "representation string" from the object (i.e. the string including a set of quotes). In a nutshell, the difference between str and repr should be that str is easy to understand for the user and repr is easy to understand for Python.

Also, if you enter any expression in the interactive shell, Python will automatically echo the repr of the result. This can be a bit confusing: In the interactive shell, when you do print(x), what you see is str(x); when you use str(x), what you see is repr(str(x)), and when you use repr(x), you see repr(repr(x)) (thus the double quotes).

>>> print("some string") # print string, no result to echo
some string
>>> str("some string") # create string, echo result
'some string'
>>> repr("some string") # create repr string, echo result
"'some string'"

What's the difference on docstrings with triple SINGLE quotes and triple DOUBLE quotes?

No. They are the same. The only difference is that the first one can contain a sequence of three unescaped double quotes, while the second can contain a sequence of three unescaped single quotes. (In other words, because the delimiters are different, there is a slight difference in what characters you can use inside them.)

Docstrings are just regular strings, and in Python there is no difference between the different string delimiters, except that, of course, you can't use the string delimiter inside the string.



Related Topics



Leave a reply



Submit