I wasn't able to find anything on this... well, I wasn't sure what to search for. But I'm curious what the advantage of this:
age = 3
print "Hello, I am %d years old." % age
...is over this:
age = 3
print "Hello, I am", age, "years old."
I wasn't able to find anything on this... well, I wasn't sure what to search for. But I'm curious what the advantage of this:
age = 3
print "Hello, I am %d years old." % age
...is over this:
age = 3
print "Hello, I am", age, "years old."
You can specify formatting options when u use %d, for exampe
%3d ---> this would occupy 3 spaces on the left side of the number being printed
%-3d --->> this would leave 3 spaces on the right of th number being printed
Printing with a format string comes in handy with tables. Let's say you have a list of player scores:
player_scores = [
["Freddy", 9234],
["Grace", 8723],
["Nutty Fuddy", 7143],
["Lurcher", 9545]]
for ps in player_scores:
player = ps[0]
score = ps[1]
print player, score
"""
somewhat ugly:
Freddy 9234
Grace 8723
Nutty Fuddy 7143
Lurcher 9545
"""
print "-"*50
for ps in player_scores:
player = ps[0]
score = ps[1]
print "%-12s %d" % (player, score)
"""
much nicer table:
Freddy 9234
Grace 8723
Nutty Fuddy 7143
Lurcher 9545
"""
Thanks a lot for the feedback. Knowing about the table formatting helps me with a current project I'm working on.
Just to clarify, the number (such as %-12) allows 12 spaces for the text to be displayed, correct? It's not 12 spaces after the text entered... what happens if your text is longer than 12 characters?
You may want to change the 12 to more spaces, or the scores gets pushed to the right, still readable, just not as pretty.
Here is an another example of formatting:
def format_dollar(amount):
"""formats amount to dollars and cents"""
return "$%.2f" % amount
print format_dollar(123.9 * 0.07) # $8.67
I make my students learn both interpolation (%) *and* comma syntax, because both are useful.
The % is my preferred way of printing numbers, floats, and strings because
(a) It allows you to print without annoying extra spaces, and
(b) It allows formatting, as mentioned above.
The following output is *nearly impossible* without using interpolation:
print "Your bill is $%0.2f." % (bill)
Your bill is $20.50.
OTOH, comma syntax is essential for
(a) supressing newlines, or
(b) printing objects that can't be interpolated, like lists and tuples
# print a list on one line:
primes = [2,3,5,7,11]
for i in primes:
print i,
2 3 5 7 11
print "The list is", primes
The list is [2,3,5,7,11]
One more trick to add to your bag: the concatenation operator:
d = 1500
print "This is another way to print the number " + str(d) + "."
Jeff
Might be a little much to digest, but here it is ...
# when you set up variables like ...
book = "Python for Nuts"
pages = 223
scripts = 77
# Python keeps this information in a local dictionary called up with vars()
print vars() # { ... , 'pages': 223, 'book': 'Python for Nuts', 'scripts': 77, ... }
# now you can show the results like this ...
print "%s has %s pages and contains a total of %s code examples" % (book, pages, scripts)
# or take advantage of vars() ...
print "%(book)s has %(pages)s pages and contains a total of %(scripts)s code examples" % vars()
You can use this method to display key:value pairs of any dictionary. Also note that %s can be used for strings and numbers.
Wow. Thanks for the feedback.
I usually use the commas or concatentation (with str()), but it sounds like the other way is better. I've used it a couple times, but I didn't understand the advantagess.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.