This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Learn Python the Hard Way

My recent threads

You haven't posted any discussions yet.

Recently updated threads

Exercise 8

Go back to: General discussion

Here is my code with commentaries and output for exercise 8.

CODE

# formatter will be our variable with 4 places
formatter = "%r %r %r %r"
 
# here we call formatter to print 4 numbers
print formatter % (1, 2 , 3, 4)
 
# here we call formatter to print four words
print formatter % ("one", "two", "three", "four")
 
# here we call formatter to print four values
print formatter % (True, False, False, True)
 
# here we call formatter to print itself 4 times
print formatter % (formatter, formatter, formatter, formatter)
 
# here we call formatter to print 4 strings
print formatter % (
 
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
 
)

OUTPUT

1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'

EXTRA CREDIT

I don't know why the "But it didn't sing." was printed with double quotes and the other strings with single quotes.
It doesn't make sense to me because all of them came by %r. 
Anyone?

 
ZM L's picture
ZM L
Thu, 2011-01-27 05:48

Well because "didn't" had a single quote in it, If they print it with single qute then your string would be 'But it didn' instead.

Rodrigo Zerbini's picture
Rodrigo Zerbini
Thu, 2011-01-27 06:49

Thanks ZM! I got it!

I've made some tests and it worked like you said: usually the strings are printed with single quotes, but for those with single quotes the terminal prints with double quotes.

michael chang's picture
michael chang
Wed, 2011-02-02 23:07

So if you want to print a single quote, you need to wrap your variable with double quote like the exercise: "didn't"

If you want to print a double quote, you need to wrap your variable with single quote like: '"okay?"'

What if you want to print the following (including all punctuation): "What's up?"

nagarjuna c's picture
nagarjuna c
Thu, 2011-02-03 12:43

formatter ="%r %r %r %r"
print formatter % (1,2,3,4)
print formatter % ('one',"two","three","four")
print formatter % (True, False, False, True)
print formatter % ( formatter,formatter,formatter,formatter)
print formatter % (
"I ahev this thing",
"That you could type up right",
"But it dint sing",
'so I said goodnight.'
)

thats my code and

my out put as

C:\lphw>python ex8.py
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I ahev this thing' 'That you could type up right' 'But it dint sing' 'so I said
goodnight.'

C:\lphw>python ex8.py
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I ahev this thing' 'That you could type up right' 'But it dint sing' 'so I said
goodnight.'

C:\lphw>python ex8.py
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I ahev this thing' 'That you could type up right' 'But it dint sing' 'so I said
goodnight.'

but i dont undersnad the out of mine is different from the author

Red Olaf's picture
Red Olaf
Fri, 2011-02-04 02:30

You misspelled "didn't" as "dint" in your code so you won't get the double quotes on that string, I believe.

nagarjuna c's picture
nagarjuna c
Fri, 2011-02-04 08:02

Yes you are right. Now i got double quotes. Can you tell me if i am correct to assume that.. when there is single quote inside a string the whole string will be displayed in double quote .. am i correct.

Dorene DeMars's picture
Dorene DeMars
Wed, 2011-02-09 08:36

Here is my code...ran fine the first time. I played around with the ' in the contraction in the last line and that made the difference for whether or not I got the double quotes.

formatter = "%r %r %r %r"

print formatter % (1,2,3,4)
print formatter % ("one", "two", "three", "four")
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)