PYTHON PROGRAMMING
So, Single (‘) or Double (“) Quotes in Python?
Many claim you should prefer single over double quotes in Python. Should you?
I started learning Python after 16 years of using R as my only programming language. Both are interpreted languages, both are intensively used by data scientists, and both use some similar operators and functions (like sum()
). But still, Python and R are two different languages, differing in many aspects.
Among the differences, there was one I considered significant — even though the truth is, it’s far from being anything close to significant. This difference lied in the use of quotes. In R, you should prefer double quotes; so, you should first use double quotes and single quotes inside them, like in "The string was 'whatever'"
; and when you’re defining a string literal, you should use double quotes:
# In R
method = "double quotes"
Unfortunately, and however strange that can sound, one of the first things I learned about Python was that you should use the opposite order of quotes than that in R — first single and then double quotes, like in 'The string was "whatever"'
. So, according to this recommendation, a simple string literal would be formatted in Python like this: