How can I format unicode strings?

Discussion in 'Python' started by gentlestone, Sep 9, 2009.

  1. gentlestone

    gentlestone Guest

    return u"{}".format(self.name)

    this one doesn't work on unicode strings. I there a not old formatting
    style possibilty for unicode strings?

    Note: self.name can be unicode string!
    gentlestone, Sep 9, 2009
    #1
    1. Advertising

  2. gentlestone schrieb:
    > return u"{}".format(self.name)


    u"{0}".format(u"blah")

    works for me with python-2.6.2
    Maybe your format string is wrong.

    - Patrick
    Patrick Sabin, Sep 9, 2009
    #2
    1. Advertising

  3. gentlestone <> writes:

    > return u"{}".format(self.name)
    >
    > this one doesn't work on unicode strings. I there a not old formatting
    > style possibilty for unicode strings?


    It looks like you're trying to mix python 3.1 and 2.6. In 2.6 you have
    to put a number inside the {} to tell it which argument to use. In 3.1
    all strings are unicode.

    Apparently when 2.7 is released it will backport the empty {} feature
    from 3.1. Until then

    return u'{0}'.format(self.name)

    is what you should probably use.

    Tim.
    Tim Northover, Sep 9, 2009
    #3
  4. gentlestone

    gentlestone Guest

    On 9. Sep., 12:31 h., Tim Northover <>
    wrote:
    > gentlestone <> writes:
    > >  return u"{}".format(self.name)

    >
    > > this one doesn't work on unicode strings. I there a not old formatting
    > > style possibilty for unicode strings?

    >
    > It looks like you're trying to mix python 3.1 and 2.6. In 2.6 you have
    > to put a number inside the {} to tell it which argument to use. In 3.1
    > all strings are unicode.
    >
    > Apparently when 2.7 is released it will backport the empty {} feature
    > from 3.1. Until then
    >
    > return u'{0}'.format(self.name)
    >
    > is what you should probably use.
    >
    > Tim.


    I have python 2.5

    return u'{0}'.format(self.name)

    doesn't work eigther

    the error message i've got is:

    'unicode' object has no attribute 'format'

    is the new formatting style newer then python 2.5?
    gentlestone, Sep 9, 2009
    #4
  5. On 9 Sep, 12:49, gentlestone <> wrote:
    >
    > I have python 2.5
    >
    > return u'{0}'.format(self.name)
    >
    > doesn't work eigther
    >
    > the error message i've got is:
    >
    > 'unicode' object has no attribute 'format'
    >
    > is the new formatting style newer then python 2.5?


    Yes. The new string formatting appeared in python 2.6. Perhaps there
    is some __future__ stuff you can import to get it to work, don't know.
    If not you are stuck with the old string formatting until you upgrade
    to 2.6 or newer:

    >>> print u'Hello %s!' % u'world'

    Hello world!

    /Niklas Norrthon
    Niklas Norrthon, Sep 9, 2009
    #5
    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. Dennis Benzinger
    Replies:
    1
    Views:
    412
    John Machin
    Aug 7, 2006
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    740
    Malcolm
    Jun 24, 2006
  3. Asterix
    Replies:
    5
    Views:
    702
    Matt Nordhoff
    Aug 31, 2008
  4. Replies:
    0
    Views:
    80
  5. David M. Cotter
    Replies:
    19
    Views:
    246
    David M. Cotter
    Aug 28, 2013
Loading...

Share This Page