Re: raw format string in string format method?

Discussion in 'Python' started by Chris Angelico, Feb 28, 2013.

  1. On Fri, Mar 1, 2013 at 1:11 AM, Helmut Jarausch <> wrote:
    > Hi,
    >
    > I'd like to print a string with the string format method which uses
    > {0}, ...
    >
    > Unfortunately, the string contains TeX commands which use lots of
    > braces. Therefore I would have to double all these braces just for the
    > format method which makes the string hardly readable.
    >
    > Is there anything like a "raw" format string and any other means
    > to circumvent this?


    You could use a different string formatting function, such as
    percent-formatting:

    "Hello, {0}, this is %s" % some_string

    The {0} will be output literally, and the %s will be replaced by the
    string. Braces are ignored, percent signs are significant.

    ChrisA
     
    Chris Angelico, Feb 28, 2013
    #1
    1. Advertising

  2. On Fri, Mar 1, 2013 at 1:42 AM, Helmut Jarausch <> wrote:
    > On Fri, 01 Mar 2013 01:22:48 +1100, Chris Angelico wrote:
    >
    >> On Fri, Mar 1, 2013 at 1:11 AM, Helmut Jarausch <>
    >> wrote:
    >>> Hi,
    >>>
    >>> I'd like to print a string with the string format method which uses
    >>> {0}, ...
    >>>
    >>> Unfortunately, the string contains TeX commands which use lots of
    >>> braces. Therefore I would have to double all these braces just for the
    >>> format method which makes the string hardly readable.
    >>>
    >>> Is there anything like a "raw" format string and any other means to
    >>> circumvent this?

    >>
    >> You could use a different string formatting function, such as
    >> percent-formatting:
    >>
    >> "Hello, {0}, this is %s" % some_string
    >>
    >> The {0} will be output literally, and the %s will be replaced by the
    >> string. Braces are ignored, percent signs are significant.
    >>
    >> ChrisA

    >
    > Originally I had used percent-formatting
    > But isn't it deprecated in Python 3.X ?


    No, it's still fully supported. Every now and then someone suggests
    that it's worse than .format() for some reason or another, but the
    fact is that it's not going away. Use whichever one makes the most
    sense.

    Yes, this is a slight violation of "one obvious way to do it". But
    it's handy to have both format methods; they have overlapping but
    distinct feature sets.

    ChrisA
     
    Chris Angelico, Feb 28, 2013
    #2
    1. Advertising

  3. Chris Angelico

    Rick Johnson Guest

    On Thursday, February 28, 2013 8:42:31 AM UTC-6, Helmut Jarausch wrote:

    > Originally I had used percent-formatting
    > But isn't it deprecated in Python 3.X ?


    I don't know the current state of "percent formats" future, however, i can tell you that it should be deprecated ASAP!

    Simpleminded Sam blubbered: """ But rick, how will we format strings that contain curly braces without the "percent format" option? We can't live without "percent formatting", we are but simple minded folks! *sky-falling* """

    Easy Sam. You allow the user to pass "opener" and "closer" chars to the format function.

    >>> "Elvis {has} <0> the {{{ building".format('left', opener='<', closer='>')

    'Elvis {has} left the {{{ building'

    For strings that contain all braces you could even create something unique:

    >>> "(Elvis) {has} {S}0{E} the <building>".format('left', opener='{S}', closer='{E}')

    '(Elvis) {has} left the <building>'
     
    Rick Johnson, Feb 28, 2013
    #3
  4. On 28/02/2013 14:42, Helmut Jarausch wrote:
    > On Fri, 01 Mar 2013 01:22:48 +1100, Chris Angelico wrote:
    >
    >> On Fri, Mar 1, 2013 at 1:11 AM, Helmut Jarausch <>
    >> wrote:
    >>> Hi,
    >>>
    >>> I'd like to print a string with the string format method which uses
    >>> {0}, ...
    >>>
    >>> Unfortunately, the string contains TeX commands which use lots of
    >>> braces. Therefore I would have to double all these braces just for the
    >>> format method which makes the string hardly readable.
    >>>
    >>> Is there anything like a "raw" format string and any other means to
    >>> circumvent this?

    >>
    >> You could use a different string formatting function, such as
    >> percent-formatting:
    >>
    >> "Hello, {0}, this is %s" % some_string
    >>
    >> The {0} will be output literally, and the %s will be replaced by the
    >> string. Braces are ignored, percent signs are significant.
    >>
    >> ChrisA

    >
    > Originally I had used percent-formatting
    > But isn't it deprecated in Python 3.X ?
    >
    > Thanks,
    > Helmut.
    >


    Nope please see
    http://mail.python.org/pipermail/python-dev/2012-February/116790.html

    --
    Cheers.

    Mark Lawrence
     
    Mark Lawrence, Mar 1, 2013
    #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. Ven
    Replies:
    6
    Views:
    518
  2. bharath
    Replies:
    4
    Views:
    3,184
    Thomas Fritsch
    Mar 16, 2007
  3. ankur
    Replies:
    1
    Views:
    12,784
    Jan =?UTF-8?B?VGhvbcOk?=
    Aug 27, 2007
  4. Peter Otten
    Replies:
    0
    Views:
    133
    Peter Otten
    Feb 28, 2013
  5. Rick Johnson
    Replies:
    0
    Views:
    140
    Rick Johnson
    Feb 28, 2013
Loading...

Share This Page