print with variable justification (with *)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Why Tea

    #1

    print with variable justification (with *)

    print format % values
    An optional minimum width of the conversion, specified using one or
    more digits or an asterisk (*), which means that the width is taken
    from the next item in values

    That's from one of O'reilly's books. But there is no example and I
    couldn't get it to work by trials and errors. Does anyone have a
    working example?

    /Why Tea

  • Marc 'BlackJack' Rintsch

    #2
    Re: print with variable justification (with *)

    In <1163883589.401 180.271190@m73g 2000cwd.googleg roups.com>, Why Tea wrote:
    print format % values
    An optional minimum width of the conversion, specified using one or
    more digits or an asterisk (*), which means that the width is taken
    from the next item in values
    >
    That's from one of O'reilly's books. But there is no example and I
    couldn't get it to work by trials and errors. Does anyone have a
    working example?
    In [50]: '%*s' % (5, 'spam')
    Out[50]: ' spam'

    In [51]: '%*s' % (10, 'spam')
    Out[51]: ' spam'

    In [52]: '%*s' % (15, 'spam')
    Out[52]: ' spam'

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • tom

      #3
      Re: print with variable justification (with *)

      Why Tea wrote:
      print format % values
      An optional minimum width of the conversion, specified using one or
      more digits or an asterisk (*), which means that the width is taken
      from the next item in values
      >
      That's from one of O'reilly's books. But there is no example and I
      couldn't get it to work by trials and errors. Does anyone have a
      working example?
      >
      /Why Tea
      >
      >
      value = 3.141592654
      print "%1.3f" % value


      there you go :) look at printf in c for general ideas about the format
      specifiers

      Comment

      • Why Tea

        #4
        Re: print with variable justification (with *)

        In [50]: '%*s' % (5, 'spam')
        Out[50]: ' spam'
        >
        Marc, that's exactly what I need. Thanks!

        /Why Tea

        Comment

        • John Machin

          #5
          Re: print with variable justification (with *)

          tom wrote:
          Why Tea wrote:
          print format % values
          An optional minimum width of the conversion, specified using one or
          more digits or an asterisk (*), which means that the width is taken
          from the next item in values

          That's from one of O'reilly's books. But there is no example and I
          couldn't get it to work by trials and errors. Does anyone have a
          working example?

          /Why Tea
          value = 3.141592654
          print "%1.3f" % value
          Please consider reading the subject of a message occasionally :)

          | >>value = 3.141592654
          | >>print "%1.3f" % value
          | 3.142
          | >>print "%10.3f" % value
          | 3.142
          | >>print "%*.3f" % (1, value)
          | 3.142
          | >>print "%*.3f" % (10, value)
          | 3.142
          | >>for n in range(11):
          | ... print "%*.3f" % (n, value)
          | ...
          | 3.142
          | 3.142
          | 3.142
          | 3.142
          | 3.142
          | 3.142
          | 3.142
          | 3.142
          | 3.142
          | 3.142
          | 3.142
          | >>>
          there you go :)
          There *you* go :)
          look at printf in c for general ideas about the format
          specifiers
          Unfortunately this is about the same advice as given by the official
          Python tutorial:

          """Most formats work exactly as in C [snip] Using * to pass the width
          or precision in as a separate (integer) argument is supported"""

          Not quite so many folks come to Python with a background in C these
          days. Is anyone aware of a tutorial that covers % formatting from a
          standing start?

          Cheers,
          John

          Comment

          • tom

            #6
            Re: print with variable justification (with *)

            John Machin wrote:
            tom wrote:
            >
            >Why Tea wrote:
            >>
            >>print format % values
            >>An optional minimum width of the conversion, specified using one or
            >>more digits or an asterisk (*), which means that the width is taken
            >>from the next item in values
            >>>
            >>That's from one of O'reilly's books. But there is no example and I
            >>couldn't get it to work by trials and errors. Does anyone have a
            >>working example?
            >>>
            >>/Why Tea
            >>>
            >>>
            >>>
            >value = 3.141592654
            >print "%1.3f" % value
            >>
            >
            Please consider reading the subject of a message occasionally :)
            >
            Sorry bout that !
            | >>value = 3.141592654
            | >>print "%1.3f" % value
            | 3.142
            | >>print "%10.3f" % value
            | 3.142
            | >>print "%*.3f" % (1, value)
            | 3.142
            | >>print "%*.3f" % (10, value)
            | 3.142
            | >>for n in range(11):
            | ... print "%*.3f" % (n, value)
            | ...
            | 3.142
            | 3.142
            | 3.142
            | 3.142
            | 3.142
            | 3.142
            | 3.142
            | 3.142
            | 3.142
            | 3.142
            | 3.142
            | >>>
            >
            >there you go :)
            >>
            >
            There *you* go :)
            >
            >
            > look at printf in c for general ideas about the format
            >specifiers
            >>
            >
            Unfortunately this is about the same advice as given by the official
            Python tutorial:
            >
            """Most formats work exactly as in C [snip] Using * to pass the width
            or precision in as a separate (integer) argument is supported"""
            >
            Not quite so many folks come to Python with a background in C these
            days. Is anyone aware of a tutorial that covers % formatting from a
            standing start?
            >
            >
            If you're on a unix system you can probably do `man fprintf`, but it
            will mean wading through a lot of stuff you're no interested in. This
            has a good little section on it though, specifically for python

            Python 2.4 language Quick Reference, freely available in HTML (several styles) and PDF versions.

            Cheers,
            John
            >
            >

            Comment

            • John Machin

              #7
              Re: print with variable justification (with *)


              tom wrote:
              John Machin wrote:
              Not quite so many folks come to Python with a background in C these
              days. Is anyone aware of a tutorial that covers % formatting from a
              standing start?
              If you're on a unix system you can probably do `man fprintf`, but it
              will mean wading through a lot of stuff you're no interested in. This
              has a good little section on it though, specifically for python
              >
              Python 2.4 language Quick Reference, freely available in HTML (several styles) and PDF versions.

              >
              Well done, tom, you've redeemed yourself! Those formatting examples are
              just the ticket. I used that quick reference years ago when I started
              out with Python, but I wasn't aware that it was still maintained, and
              an advertisement for CSS as well. Bonus!

              Cheers,
              John

              Comment

              Working...