Table des matières

Gérer l'affichage avec Python

format (python 3)

Exemples possible d'utilisation :

print('{0:<8} {1:>5} {2:>5} {3:>5} {4:>5.1f} {5:>5} {6}'.format(username, pid, p.ppid, p.get_cpu_percent(interval=0), p.get_memory_percent(), p.get_memory_info().rss,  p.name))
adresse = """
  {no_rue}, {nom_rue}
  {code_postal} {nom_ville} ({pays})
""".format(no_rue=5, nom_rue="rue des Postes", code_postal=75003, \
    nom_ville="Paris", pays="France")
print(adresse)
print("hello {name} you are {age} years old".format(**locals()))

A partir de Python 3.6 on peut utiliser les f-strings :

>>> f"The \"comedian\" is {name}, aged {age}."
'The "comedian" is Eric Idle, aged 74.'

% (python2)

>> print ( "%5s" % ('x',) )
>>> print "Location: %-*s  Revision: %s" % (20,"10-10-10-10","1")
Location: 10-10-10-10           Revision: 1
>>> print "District: %-*s  Date: %s" % (20,"Tower","May 16, 2012")
District: Tower                 Date: May 16, 2012