[Tutor] string formatting (%s)

Alan Gauld alan.gauld at freenet.co.uk
Sun Apr 16 09:42:39 CEST 2006


> Can I use string formatting in my own functions, or is it stuck with 
> builtins?

No, you can use it too.

> It doesn't make much sense for me, but I don't understand the following
> error:

> In [2]: def printd(arg):
>   ...:     print('>>> %s') % arg

The format operator needs to be with the string, inside the parentheses:

def printd(arg):
   print('>>> %s' % arg)

> TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

The NoneType it refers to is the None returned by the function.
In your code the function gets called then Python tries to apply
string formatting to the result, which is  None.

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list