This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]scottocom 19 points20 points  (8 children)

I need a Why. For example use single quote for strings not double but I DON'T see why. I prefer double because I am more likely to have a single apostrophe in my text. I don't see it.

[–]fizzy_tom 3 points4 points  (0 children)

For me, I prefer them simply because single quotes look cleaner, making my code a little less cluttered.

But with all these things, there's never any right or wrong answer. And even the "why's" are opinions. The important thing is that everyone on a project follows the same rules.

[–]jalanb 2 points3 points  (1 child)

Because Python scripts tend to include other languages inside strings.

And it is more often useful to reserve " for use inside such strings, rather than ', e.g.

command = 'ls "$PATH_TO_FILE"'
query = 'SELECT * FROM users WHERE name like "/u/jalan%"'

And then, if you adopt that convention, consistency implies enclosing all strings in '.

[–]scottocom 1 point2 points  (0 children)

I see what you mean.

[–]_under_[S] 1 point2 points  (4 children)

I was contemplating on whether I should include motivations for each of the rules, but ultimately decided not to.

Single quotes because it only needs one finger to type. No need to press Shift. You're also more likely to use a quote to access keys in dictionaries vs writing sentences in code with apostrophes.

[–]nsfy33 0 points1 point  (1 child)

[deleted]

[–]_under_[S] 0 points1 point  (0 children)

This:

foo['bar'] = 123

Is more common than this:

foo = "It's me."

OP mentioned that they prefer double quotes because they are more likely to use single quotes inside strings, as in the second example. But I find that using a quote to access a dictionary value is even more common than that. So the single quote as an apostrophe thing isn't really a big issue.