Posts: 2
Threads: 2
Joined: Aug 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
Posts: 12,022
Threads: 484
Joined: Sep 2016
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
Posts: 7,312
Threads: 123
Joined: Sep 2016
(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
Posts: 3,458
Threads: 101
Joined: Sep 2016
(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.
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 5,151
Threads: 396
Joined: Sep 2016
Aug-31-2017, 09:29 PM
(This post was last modified: Aug-31-2017, 09:30 PM by metulburr.)
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.
Recommended Tutorials:
Posts: 12,022
Threads: 484
Joined: Sep 2016
Aug-31-2017, 11:52 PM
(This post was last modified: Aug-31-2017, 11:53 PM by Larz60+.)
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.'
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 5,151
Threads: 396
Joined: Sep 2016
Sep-01-2017, 01:54 AM
(This post was last modified: Sep-01-2017, 01:55 AM by metulburr.)
or
poor_jack = '''My first name is "Jack" but I can't remember my Last name.'''
Recommended Tutorials:
Posts: 2,953
Threads: 48
Joined: Sep 2016
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.
|