String formatting with %s

Discussion in 'Python' started by Donn Ingle, Dec 2, 2007.

  1. Donn Ingle

    Donn Ingle Guest

    Hi,
    I'm sure I one read somewhere that there is a simple way to set the order
    of replacements withing a string *without* using a dictionary.

    What I mean is:
    >>> s = "%s and %s" % ( "A", "B" )
    >>> print s

    A and B

    Now, is there something quick like:
    >>> s = "%s/2 and %s/1" % ( "A", "B" )
    >>> print s

    B and A

    ?

    I know it can be done with a dict:
    d = { "one" : "A", "two" : "B" }
    s = "%(two)s and %(one)s" % d


    \d
     
    Donn Ingle, Dec 2, 2007
    #1
    1. Advertising

  2. Donn Ingle

    Mel Guest

    Donn Ingle wrote:
    > Now, is there something quick like:
    >>>> s = "%s/2 and %s/1" % ( "A", "B" )
    >>>> print s

    > B and A
    >
    > ?


    GNU glibc printf accepts a format string:

    printf ("%2$s and %1$s", "A", "B");

    to produce what you want -- but not Python, AFAIK.


    Mel.
     
    Mel, Dec 2, 2007
    #2
    1. Advertising

  3. Donn Ingle

    Donn Ingle Guest

    > but not Python, AFAIK
    Damn!

    \d
     
    Donn Ingle, Dec 2, 2007
    #3
  4. Donn Ingle

    MRAB Guest

    On Dec 2, 1:35 pm, Donn Ingle <> wrote:
    > Hi,
    > I'm sure I one read somewhere that there is a simple way to set the order
    > of replacements withing a string *without* using a dictionary.
    >
    > What I mean is:>>> s = "%s and %s" % ( "A", "B" )
    > >>> print s

    >
    > A and B
    >
    > Now, is there something quick like:>>> s = "%s/2 and %s/1" % ( "A", "B" )
    > >>> print s

    >
    > B and A
    >
    > ?
    >
    > I know it can be done with a dict:
    > d = { "one" : "A", "two" : "B" }
    > s = "%(two)s and %(one)s" % d
    >
    > \d


    One quick solution is to write a function for it:

    def format(template, values):
    import re
    # Collect the indexes from the template.
    order = map(int, re.findall(r'%\[(\d+)\]', template))
    # Remove the indexes from the template.
    template = re.sub(r'(?<=%)\[(\d+)\]', '', template)
    # Create a tuple containing the values in the correct positions.
    values = tuple(values[index] for index in order)
    return template % values

    >>> format("%[0]s and %[1]s", ("A", "B"))

    'A and B'
    >>> format("%[1]s and %[0]s", ("A", "B"))

    'B and A'
     
    MRAB, Dec 2, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lee Simpson

    Re: Formatting string in WHERE clause

    Lee Simpson, Jul 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,263
    Lee Simpson
    Jul 18, 2003
  2. Steve Hoyer

    Re: Formatting String in Datagrid

    Steve Hoyer, Apr 14, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,322
    Steve Hoyer
    Apr 15, 2004
  3. Andre Alexander Bell

    String formatting with the format string syntax

    Andre Alexander Bell, Sep 14, 2010, in forum: Python
    Replies:
    9
    Views:
    506
    Andre Alexander Bell
    Sep 15, 2010
  4. Andrew Berg
    Replies:
    13
    Views:
    1,302
    Andrew Berg
    Jul 11, 2011
  5. Victor Hooi
    Replies:
    3
    Views:
    118
    Chris Kaynor
    Nov 27, 2013
Loading...

Share This Page