PYTHON PROGRAMMING

So, Single (‘) or Double (“) Quotes in Python?

Many claim you should prefer single over double quotes in Python. Should you?

Marcin Kozak
PythonIQ
Published in
10 min readDec 30, 2022

--

Photo by Miguel Luis on Unsplash

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:

--

--

Marcin Kozak
PythonIQ

A full professor, interdisciplinary researcher, data scientist, statistician, Python, R and Go developer, open-source contributor — and a devoted writer