6 Replies - 14853 Views - Last Post: 18 July 2011 - 05:18 AM Rate Topic: -----

#1 monster92   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 10-May 11

Difference Between Triple Single Quotes and Triple Double Quotes

Posted 15 July 2011 - 02:10 PM

The book I'm currently studying asks this question related to the following codes.
"Use (triple-single-quote) instead. Can you see why you might use that instead of """?"

Now, I made two files one with triple double quotes and one with trible single quotes. However, they output the same information. What have I done wrong here? And what is the answer to that question?


tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split \non a line."
backslash_cat = "I'm \\ a \\ cat"

fat_cat = """
I'll do a list:
\t* Cat food
\t* Fisheries
\t* Catnip\n\t* Grass   
"""

print tabby_cat
print persian_cat
print backslash_cat
print fat_cat 


tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split \non a line."
backslash_cat = "I'm \\ a \\ cat"

fat_cat = '''
I'll do a list:
\t* Cat food
\t* Fisheries
\t* Catnip\n\t* Grass   
'''

print tabby_cat
print persian_cat
print backslash_cat
print fat_cat 


The line I am referring to are 5 and 10

Is This A Good Question/Topic? 0
  • +

Replies To: Difference Between Triple Single Quotes and Triple Double Quotes

#2 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 835
  • View blog
  • Posts: 2,267
  • Joined: 23-December 08

Re: Difference Between Triple Single Quotes and Triple Double Quotes

Posted 15 July 2011 - 03:11 PM

From a programming standpoint, there is no difference. Perhaps the book worries about smart quotes? That's werid, what book is it?
Was This Post Helpful? 0
  • +
  • -

#3 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 455
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: Difference Between Triple Single Quotes and Triple Double Quotes

Posted 15 July 2011 - 03:20 PM

Well there is one difference """-quoted strings can contain ''' and '''-quoted strings can contain """.

But indeed, I tested it, it does the same. Perhaps they were expecting one of the two to take escape characters as literals and the other not. perhaps this was the case in some python versions.

This post has been edited by Karel-Lodewijk: 15 July 2011 - 03:25 PM

Was This Post Helpful? 0
  • +
  • -

#4 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 835
  • View blog
  • Posts: 2,267
  • Joined: 23-December 08

Re: Difference Between Triple Single Quotes and Triple Double Quotes

Posted 15 July 2011 - 03:25 PM

well yes, double quotes can contain single quotes and single quotes can contain double quotes... I hardly see how that makes one preferable in this situation though.
Was This Post Helpful? 0
  • +
  • -

#5 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 322
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Difference Between Triple Single Quotes and Triple Double Quotes

Posted 15 July 2011 - 03:46 PM

One thing I can think of is docstrings, being triple quoted.

It's not really a difference between them, docstrings can be either double or triple quoted, but generally they all seem to be triple quoted - in my experience.

EDIT: Ooops, I read the topic a little wrong - I'll blame the time :). Single and double triple quotes have no real difference.

This post has been edited by Simown: 15 July 2011 - 03:53 PM

Was This Post Helpful? 1
  • +
  • -

#6 sean.orear   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 12
  • Joined: 21-December 10

Re: Difference Between Triple Single Quotes and Triple Double Quotes

Posted 17 July 2011 - 07:24 AM

The only time I am aware that you need to be concerned with single v. double is if your string contains the same kind of quote (but this should not have an effect on the triple quotes). For instance:

My name as a string:
'Sean O'Rear' will some out not right. The code will see 'Sean O' as a string and the Rear' as a syntax error. I have to use "Sean O'Rear" to print my name.
Was This Post Helpful? 0
  • +
  • -

#7 Brewer   User is offline

  • Awesome
  • member icon

Reputation: 182
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Difference Between Triple Single Quotes and Triple Double Quotes

Posted 18 July 2011 - 05:18 AM

View PostKarel-Lodewijk, on 15 July 2011 - 07:50 PM, said:

Well there is one difference """-quoted strings can contain ''' and '''-quoted strings can contain """.


I think that it is worth noting that a """-quoted string can contain ''' if you escape at least one of the ' characters using a backslash:

>>> s = """Hello, I am a \"""."""
>>> s
'Hello, I am a """.'

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1