I have a string that looks like \'%s in %s\'
and I want to know how to seperate the arguments so that they are two different %s. My mind coming from Java came u
If you're using more than one argument it has to be in a tuple (note the extra parentheses):
'%s in %s' % (unicode(self.author), unicode(self.publication))
As EOL points out, the unicode()
function usually assumes ascii encoding as a default, so if you have non-ASCII characters, it's safer to explicitly pass the encoding:
'%s in %s' % (unicode(self.author,'utf-8'), unicode(self.publication('utf-8')))
And as of Python 3.0, it's preferred to use the str.format() syntax instead:
'{0} in {1}'.format(unicode(self.author,'utf-8'),unicode(self.publication,'utf-8'))
There is a significant problem with some of the answers posted so far: unicode()
decodes from the default encoding, which is often ASCII; in fact, unicode()
tries to make "sense" of the bytes it is given by converting them into characters. Thus, the following code, which is essentially what is recommended by previous answers, fails on my machine:
# -*- coding: utf-8 -*-
author = 'éric'
print '{0}'.format(unicode(author))
gives:
Traceback (most recent call last):
File "test.py", line 3, in <module>
print '{0}'.format(unicode(author))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
The failure comes from the fact that author
does not contain only ASCII bytes (i.e. with values in [0; 127]), and unicode()
decodes from ASCII by default (on many machines).
A robust solution is to explicitly give the encoding used in your fields; taking UTF-8 as an example:
u'{0} in {1}'.format(unicode(self.author, 'utf-8'), unicode(self.publication, 'utf-8'))
(or without the initial u
, depending on whether you want a Unicode result or a byte string).
At this point, one might want to consider having the author
and publication
fields be Unicode strings, instead of decoding them during formatting.
For python2 you can also do this
'%(author)s in %(publication)s'%{'author':unicode(self.author),
'publication':unicode(self.publication)}
which is handy if you have a lot of arguments to substitute (particularly if you are doing internationalisation)
Python2.6 onwards supports .format()
'{author} in {publication}'.format(author=self.author,
publication=self.publication)
You must just put the values into parentheses:
'%s in %s' % (unicode(self.author), unicode(self.publication))
Here, for the first %s
the unicode(self.author)
will be placed. And for the second %s
, the unicode(self.publication)
will be used.
Note: You should favor
string formatting
over the%
Notation. More info here
For completeness, in python 3.6 f-string are introduced in PEP-498. These strings make it possible to
embed expressions inside string literals, using a minimal syntax.
That would mean that for your example you could also use:
f'{self.author} in {self.publication}'
You could also use it clean and simple (but wrong! because you should use format
like Mark Byers said) by doing:
print 'This is my %s formatted with %d arguments' % ('string', 2)