Python Forum
Best Practice For String Quotations ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Best Practice For String Quotations ? (/thread-4646.html)



Best Practice For String Quotations ? - Zork_3 - Aug-31-2017

Hello ,

I am still very new to Python and programming in general .

I have noticed that some programmers use " " and some use ' ' in strings.

Example:

Print(" hello " )
Print('hello' )

While I understand that both are correct and if you want to use the single quote in your string you have to use the double quotes to define your string , as in :

Print(" it's a nice day today " )

I would like to know is there a standard that most Of you use ?
It seems to me that double quotes would be best, but I am very new and I want to make sure that I am not starting out with any bad habits .

Thanks


RE: Best Practice For String Quotations ? - Larz60+ - Aug-31-2017

It depends, and is sort of a personal preference.
The actual pep (most recent) can be viewed here: https://www.python.org/dev/peps/pep-0498/
I like to use single quotes, but use double if string contains single quote, or vice-versa,
(single quote if string contains double quote). If the string contains both single or double, the one
that is the same as that selected for the whole string will have to be escaped,example: '' for a single quote,
or "" for double quote


RE: Best Practice For String Quotations ? - snippsat - Aug-31-2017

(Aug-31-2017, 04:12 PM)Zork_3 Wrote: I would like to know is there a standard that most Of you use ?
PEP8.org (Better CSS styled pep-8 website).
String Quotes 
Quote:In Python, single-quoted strings and double-quoted strings are the same.
This PEP does not make a recommendation for this.
Pick a rule and stick to it.
When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string.
It improves readability.
For triple-quoted strings, always use double quote characters to be consistent with the docstring convention in PEP 257.

Zork_3 Wrote:It seems to me that double quotes would be best
My rule is that i always use single quotes,easier to type and i think it look better Wink


RE: Best Practice For String Quotations ? - nilamo - Aug-31-2017

(Aug-31-2017, 04:55 PM)snippsat Wrote: PEP8.org (Better CSS styled pep-8 website).
Except for the comments.  That font is barely legible.

In python, it doesn't matter.  In some languages, it does.  Php, for example, everything should be single quotes, as double quotes allow for string interpolation (the same as a python f-string), and thus take slightly longer to parse.

I prefer double-quoted.  I'm not sure I can explain why, as I frequently need to use single-quoted anyway to include xml attributes.


RE: Best Practice For String Quotations ? - ichabod801 - Aug-31-2017

I use single quotes, but that's because I did a lot of SAS programming. Double quoted text in SAS is checked by the macro processor, which may do things you don't want, so you only use double quotes when you mean it. But it generally doesn't matter.


RE: Best Practice For String Quotations ? - metulburr - Aug-31-2017

I use single quotes. If there is a single quote in the string, i use double quotes. If there is both, i use triple quotes. I prefer triple quotes over escaping string quotes as i think it looks better and less chance of messing it up.


RE: Best Practice For String Quotations ? - Larz60+ - Aug-31-2017

Quote:My rule is that i always use single quotes,easier to type and i think it look better
I personally agree, but you will still run into strings like:
# The string: My first name is Jack but I can't remember my Last name.
poor_jack = "My first name is Jack but I can't remember my Last name."
The alternative is:
poor_jack = 'My first name is Jack but I can''t remember my Last name.'



RE: Best Practice For String Quotations ? - ichabod801 - Sep-01-2017

Or:

poor_jack = 'My first name is Jack but I can\'t remember my Last name.'
or:

poor_jack = 'My first name is Jack but I cannot remember my Last name.'
I am Jack's forgotten last name.


RE: Best Practice For String Quotations ? - metulburr - Sep-01-2017

or
poor_jack = '''My first name is "Jack" but I can't remember my Last name.'''



RE: Best Practice For String Quotations ? - wavic - Sep-01-2017

I use single quotes because I don't have to press shift key in addition. If I don't have single quotes in the string. Sometimes I use double quotes just because in my language it's natural. When I have both, single and double quotes in the string I am using triple quotes. Escaping makes strings ugly for me.