1,105,578 Community Members

Quiz of the day: print format

Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
1
 

What does this print statement produce and why? Learned it today, I still must be a newbie.

print('%0*d' % (3, 7))
Member Avatar
m4ster_r0shi
Posting Whiz in Training
283 posts since Mar 2010
Reputation Points: 142 [?]
Q&As Helped to Solve: 42 [?]
Skill Endorsements: 3 [?]
 
0
 

Ok, since  nobody  has  replied  yet, I  thought  I'd  give  it a shot.
Warning -> I'm not an expert, at all. I just started learning python.

The above  statement   prints 7, left zero  padded,  so that  the total
number of digits is 3. You can also put 3 in the format string instead:

print('%03d' % 7)
Member Avatar
M.S.
Junior Poster
118 posts since Jul 2011
Reputation Points: 53 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 2 [?]
 
0
 

Is "d" here stands for "digit"?
this bellow code does the same but instead of 0's it prints empty spaces"

print('%0*s' % (3, 7))

what about "s" here? does it mean "string"?

Member Avatar
Gribouillis
Posting Maven
3,456 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 884 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

Is "d" here stands for "digit"?
this bellow code does the same but instead of 0's it prints empty spaces"

print('%0*s' % (3, 7))

what about "s" here? does it mean "string"?

Yes, 's' means string. In principle, the str() method is applied to the argument. Better learn to use the new format() operator which is more powerful than %. For example

>>> print( "{0:{1}^{2}s}".format(" 77 ", "=", 60) )
============================ 77 ============================

Some more examples here http://www.daniweb.com/software-development/python/code/232375

Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

It is more like 'use the objects __str__ method ie use str function to object'

How about these, then:

print('%*r' % (6, '7'))
 print('%*r' % (6, 7))

Yes, format is good to learn also, but there is lot of code with 'modulo formatting', so you better learn both.

Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

Yes, 's' means string. In principle, the str() method is applied to the argument. Better learn to use the new format() operator which is more powerful than %. For example

>>> print( "{0:{1}^{2}s}".format(" 77 ", "=", 60) )
============================ 77 ============================

Some more examples here http://www.daniweb.com/software-development/python/code/232375

Shows that the language of format is powerfull, but I would still prefer for readability:

print(' 77 '.center(60, '='))

or

print( "{info:{char}^{width}s}".format(info=" 77 ", char="=", width=60))
Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

What this new format function does and how it works?

def print_formats(num):
    print('{num:016b} {num:04x} {num:06o} {num:05d}'.format(**locals()))

print_formats(1234)
Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

It is more like 'use the objects __str__ method ie use str function to object'

How about these, then:

print('%*r' % (6, '7'))
 print('%*r' % (6, 7))

Yes, format is good to learn also, but there is lot of code with 'modulo formatting', so you better learn both.

>> print('%*r' % (6, '7'))
   '7'
>>> print('%*r' % (6, 7))
     7

So for repr the quotes count for width, so string 7 start two spaces before than number 7 to finish at same position.

And how about this then, what it does?

print('%u' % -7)
Member Avatar
M.S.
Junior Poster
118 posts since Jul 2011
Reputation Points: 53 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 2 [?]
 
0
 

I think It only accepts Integers and convert them to Unicode strings:

>>> a='%u' % -7
>>> print type(a)
<type 'str'>
Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

I think It only accepts Integers and convert them to Unicode strings:

>>> a='%u' % -7
>>> print type(a)
<type 'str'>

Nope it is not, and it is not (anymore) unsigned integer like in C, but it is synonym for %i and %d. Better to avoid using it, as it will be deprecated and is only confusing. This is only to show "Python is not C".

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

Python27 and higher ...

# pad with leading zeroes
print( "value is {v:0{digits}n}".format(v=2, digits=3) )

"""result -->
value is 002
"""
Member Avatar
Purkinje
Light Poster
26 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

I know this thread is more than 2 weeks old, but I have a question about the format() syntax.

print( "{0:{1}^{2}s}".format(" 77 ", "=", 60) )

When specifying the field_name, you don't need to place {} around the '0'. I understand that it refers to the 0th element of the list. However, the fill character field is specified here by {1}, and the width field is specified by {2}. These obviously also refer to the list passed in format(), but they include {}.

I can't find anything in the documentation to indicate why this is. Could someone explain why this syntax is used?

Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

Those two are nested inside a format specifier not information to print.

>>> print( ":{0}^{1}s".format("=", 60) )
:=^60s
>>> print( "{0:=^60s}".format(" 77 ") )
============================ 77 ============================
>>>
Member Avatar
Purkinje
Light Poster
26 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Those two are nested inside a format specifier not information to print.

That wasn't my question. I was simply questioning the syntax of the format specifier, which I could not find in the documentation. Outside of the format specifier, Python, in general, uses '{' and '}' to refer to dictionary declarations. However, within the format specifier, it refers to elements within the tuple passed into the format() method. Why use {} within the format_spec, but not the field_name?

Please see http://docs.python.org/library/string.html#format-string-syntax for the meaning of the bolded terms I used.

Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

It is dictionary in general sense, but you can access unspecified keys by numeric key:

>>> family
{'father': 'Tony', 'mother': 'Edlira'}
>>> print("Father is {father} and mother is {mother}".format(**family))
Father is Tony and mother is Edlira
Member Avatar
Purkinje
Light Poster
26 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Ah, thanks for the clarification on that. I appreciate your help! It's interesting to see what you can do with strings.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: