[Tutor] String formatting

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Sat Aug 20 17:04:21 CEST 2005


Jorge Louis De Castro a écrit :
> Hi,
> 
> 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

Well, when using the "%" operator on string always put a tuple or a
dictionnary on the RHS :

print "Connection from %s" % (info,)

Like that, even if info is iterable you'll have the representation of
info and not of its first element (if it has only one) and an error if
it has more.

> 
> Also, anyone knows how do I pass arguments to a logger?
> 
> logger.debug("Connection from:", args)
> 
> thanks and chrs
> j.
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68


More information about the Tutor mailing list