Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

3x double quotes vs single quotes, python

Name: Anonymous 2011-01-13 23:27

Quick, easy question:
In Python language:
What is the difference between using either triple `` (double quotes) over using triple ` (single quotes).
What advantages do each provide?

Name: Anonymous 2011-01-13 23:29

What is the difference between using either triple `` (double quotes) over using triple ` (single quotes).

For one, you are using triple `` (double quotes) over using trip `(single quotes).

Name: Anonymous 2011-01-13 23:34

>>2
Sigh. You know very well what I mean.
string = `````` #those are double quotes
I am giving
an example
on seperate lines
in one string
`````` #double quotes as well

In what situation would using double quotes be better or necessary, and in what situation would using single quotes be better or necessary?
Keep in mind I am of course a beginner (couldn't you tell?), so answer in simple terms or explain in detail, please.

Name: Anonymous 2011-01-13 23:35

Oh, forgot to mention in the current exercise I am doing, the string contains a single quote, but using triple single quotes at the start and end of the string do not make a difference and still output the exact same thing, which is why I'm confused as to where a difference can actually be seen.

Name: Anonymous 2011-01-13 23:36

Why do you use different symbols from what you are describing?


"""
Hello
"""



'''
Hello
'''

Name: Anonymous 2011-01-13 23:47

I had accidently hit ctrl+shift and was having difficulty lol
"""
'''
works now.
I'll post the exercise:

fat_cat = """
6 I'll do a list:
7 \t* Cat food
8 \t* Fishies
9 \t* Catnip\n\t* Grass
10 """
-------------------------------
2. Use ”’ (triple-single-quote) instead. Can you see why you might use that instead of """?

Name: Anonymous 2011-01-14 0:01

So yeah, I tried them both and they both gave me the same output. I figured typing it with ' single quotes would cause it to think the string was ending earlier, due to the ' in I'll, but nope.
Any ideas?

Name: Anonymous 2011-01-14 0:14

Name: Anonymous 2011-01-14 0:22

>>8
Thank you for that link, I will be using that site a lot from now on...however I don't see an answer to my question on there.
Also, I now have this problem (I just need help with the above question and this one, I am not gonna start spamming questions):

double = "I am 6'2\" tall." # escape double-quote inside string
single = 'I am 6\'2" tall.' # escape single-quote inside string
print "%r %r" % (double, single)

Why does that give me this output:
C:\Python27\exercises>python temp.py
'I am 6\'2" tall.' 'I am 6\'2" tall.'

Should it not be giving this instead:
C:\Python27\exercises>python temp.py
'I am 6'2\" tall.' 'I am 6\'2" tall.'

Name: Anonymous 2011-01-14 0:25

Sorry, in the second output that I am expecting, it should appear like this:
C:\Python27\exercises>python temp.py
"I am 6'2\" tall." 'I am 6\'2" tall.'
should it not?
%r writes it exactly as it is written in the code, so why is it showing me something different than what I wrote?

Name: Anonymous 2011-01-14 0:44

who the fuck designed this language

Name: Anonymous 2011-01-14 0:46

>>11
Guido Van Rossum

Name: Anonymous 2011-01-14 0:47

Lmfao.
Come on, I know someone on here can answer this, it's from like the 20th page of a complete beginner book.

Name: Anonymous 2011-01-14 0:53

>>12
Hahahaha, as if his name is Guido...
but nevermind all that, I need help here people ^.^
Just those 2 questions:
1)In what situations would using single or double quotes provide an advantage over the other (or be necessary)
2)How come:
double = "I am 6'2\" tall." # escape double-quote inside string
single = 'I am 6\'2" tall.' # escape single-quote inside string
print "%r %r" % (double, single)
#is outputting
C:\Python27\exercises>python temp.py
'I am 6\'2" tall.' 'I am 6\'2" tall.'
#instead of the exact code like it should?

Name: Anonymous 2011-01-14 1:02

Agh, I keep leaving out details, my bad.
In question 1, I meant to say using 3 single or double quotes
(''' vs """)

Name: Anonymous 2011-01-14 1:16

>>14
His name is Guido.
IHBT.

Name: Anonymous 2011-01-14 1:18

>>16
I'm not trolling. Seriously, I find it hard to believe that out of all the people on /prog/, who know things 9000x more complex than this, can not help me with a beginners problem?
I really want to figure this out before I move on, help with the 2 questions is GREATLY appreciated

Name: Anonymous 2011-01-14 1:22

>>17
Would Steve Jobbs give you ten bucks just because he has billions of dollars? Well it's the same here.

Name: Anonymous 2011-01-14 1:26

>>17
Rippa na hanashi, ``aniki''.

Name: Anonymous 2011-01-14 1:31

>>19
What?

Name: Anonymous 2011-01-14 2:42

>>20
I think that's 「cool story, ``bro''」

Name: Anonymous 2011-01-14 3:45

>>14
You use single quotes for all regular one-line strings. You use triple quotes for strings that span multiple lines, e.g. embedded SQL commands, docstrings, long output to pass to sys.stdout.write, and so forth. Why is that, you might ask? Because writing
connection.execute("""SELECT *
FROM DB
WHERE A = B
ORDER BY C"""

is a lot more comfy than writing
connection.execute("SELECT *" + \
" FROM DB " + \
" WHERE A = B " + \
" ORDER BY C"


As for your second question, your understanding of %r is flawed. Its only difference to %s is the function that is used to convert the parameter passed. %r uses repr(arg) whereas %s uses str(arg). You can look up these functions in the Python reference[1]. Now, as it happens, repr() uses single quotes to represent that the passed argument was a string, so regardless of what you wrote in your code, repr() will ALWAYS surround the string using single quotes, thus, ' in the string will always end up being escaped.

There is no way to decide what quotes were used in your code after it has been parsed (unless you want to dig into Python internals and function attributes starting with __).


___
[1] - http://docs.python.org/library/functions.html

Name: Anonymous 2011-01-14 3:47

>>22
Add ) to avoid SynaxError.

Name: Anonymous 2011-01-14 3:50

>>23
Traceback (most recent call last):
  File "
/prog/", post >>23, in <1294979275>
NameError: name 'SynaxError' is not defined

Don't change these.
Name: Email:
Entire Thread Thread List