[Tutor] String formatting

Kent Johnson kent37 at tds.net
Sat Aug 20 17:45:41 CEST 2005

Pierre Barbier de Reuille wrote:
> Jorge Louis De Castro a écrit :
>>I'm slighty confused with python's string formatting operators.
>>Why is it that this prints as a string:
>>channel, info = server.accept()
>>print "Connection from", info
>>And this doesn't?
>>channel, info = server.accept()
>>print "Connection from %s" % info

What happens when you try this?

> Well, when using the "%" operator on string always put a tuple or a
> dictionnary on the RHS :
> print "Connection from %s" % (info,)

No, you can put a single item on the right without putting it in a tuple:
 >>> print 'x = %s' % 3
x = 3

>>Also, anyone knows how do I pass arguments to a logger?
>>logger.debug("Connection from:", args)

The first string is a format string, so you could use
logger.debug("Connection from: %s", info)


More information about the Tutor mailing list