Mark Cidade's answer is right - you need to supply a tuple.
However from Python 2.6 onwards you can use format instead of %:
'{0} in {1}'.format(unicode(self.author,'utf-8'), unicode(self.publication,'utf-8'))
Usage of % for formatting strings is no longer encouraged.
Answer from Mark Byers on Stack OverflowThis method of string formatting is the new standard in Python 3.0, and should be preferred to the % formatting described in String Formatting Operations in new code.
Mark Cidade's answer is right - you need to supply a tuple.
However from Python 2.6 onwards you can use format instead of %:
'{0} in {1}'.format(unicode(self.author,'utf-8'), unicode(self.publication,'utf-8'))
Usage of % for formatting strings is no longer encouraged.
This method of string formatting is the new standard in Python 3.0, and should be preferred to the % formatting described in String Formatting Operations in new code.
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'))
You were almost there, just remove the comma (and pass in a float number, not a string):
"{0:>15.2f}".format(1464.1000000000001)
See the Format Specification Mini-Language section:
format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type] fill ::= <any character> align ::= "<" | ">" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
Breaking the above format down then:
fill: <empty>
align: < # left
sign: <not specified>
width: 15
precision: 2
type: `f`
Demo:
>>> "{0:>15.2f}".format(1464.1000000000001)
' 1464.10'
Note that for numbers, the default alignment is to the right, so the > could be omitted.
"{0:15.2f}".format(1464.1000000000001)
I always find this site useful for this stuff:
https://pyformat.info/
You could use the str.format() function, that allows named arguments, so:
'''line {0}
line {1}
line {2}'''.format(1,2,3)
You could of course extend this using Python's *args syntax to allow you to pass in a tuple or list:
args = (1,2,3)
'''line {0}
line {1}
line {2}'''.format(*args)
If you can intelligently name your arguments, the most robust solution (though the most typing-intensive one) would be to use Python's **kwargs syntax to pass in a dictionary:
args = {'arg1':1, 'arg2':2, 'arg3':3}
'''line {arg1}
line {arg2}
line {arg3}'''.format(**args)
For more information on the str.format() mini-language, go here.
The easiest way might be to use literal string interpolation (available from Python 3.6 onwards and assuming all the arguments are in scope).
cmd = f"""line {1}
line {2}
line {3}"""