Python Forum
Best Practice For String Quotations ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best Practice For String Quotations ?
#1
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
Reply
#2
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
Reply
#3
(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
Reply
#4
(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.
Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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:
Reply
#7
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.'
Reply
#8
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
or
poor_jack = '''My first name is "Jack" but I can't remember my Last name.'''
Recommended Tutorials:
Reply
#10
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best practice on using virtual environments in Python bytecrunch 6 842 Feb-14-2024, 03:22 PM
Last Post: snippsat
  best practice for import libraries and using pyinstaller aster 3 2,874 Apr-17-2021, 11:12 AM
Last Post: snippsat
  Threading best practice EvanS1 2 1,941 Apr-21-2020, 10:11 PM
Last Post: EvanS1
  How to properly close db connection, best practice t4keheart 6 3,026 Jan-24-2020, 06:58 PM
Last Post: Marbelous
  Help with string practice Hermann_Fegelein 2 2,690 Aug-15-2018, 04:56 PM
Last Post: Hermann_Fegelein

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020