How can I format unicode strings?

G

gentlestone

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!
 
P

Patrick Sabin

gentlestone said:
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
 
T

Tim Northover

gentlestone said:
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.
 
G

gentlestone

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?
 
N

Niklas Norrthon

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:
Hello world!

/Niklas Norrthon
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top