2.1. String Pprint
2.1.1. Problem
>>> DATA = [
... ('firstname', 'lastname', 'age'),
... ('Alice', 'Apricot', 30),
... ('Bob', 'Blackthorn', 31),
... ('Carol', 'Corn', 32),
... ('Dave', 'Durian', 33),
... ('Eve', 'Elderberry', 34),
... ('Mallory', 'Melon', 15),
... ]
>>>
>>> print(DATA)
[('firstname', 'lastname', 'age'), ('Alice', 'Apricot', 30), ('Bob', 'Blackthorn', 31), ('Carol', 'Corn', 32), ('Dave', 'Durian', 33), ('Eve', 'Elderberry', 34), ('Mallory', 'Melon', 15)]
2.1.2. Solution
>>> from pprint import pprint
>>>
>>> DATA = [
... ('firstname', 'lastname', 'age'),
... ('Alice', 'Apricot', 30),
... ('Bob', 'Blackthorn', 31),
... ('Carol', 'Corn', 32),
... ('Dave', 'Durian', 33),
... ('Eve', 'Elderberry', 34),
... ('Mallory', 'Melon', 15),
... ]
>>>
>>> pprint(DATA)
[('firstname', 'lastname', 'age'),
('Alice', 'Apricot', 30),
('Bob', 'Blackthorn', 31),
('Carol', 'Corn', 32),
('Dave', 'Durian', 33),
('Eve', 'Elderberry', 34),
('Mallory', 'Melon', 15)]
2.1.3. Pprint
Pretty-print a Python object to a stream [default is sys.stdout].
def pprint(object: Any,
stream: Any = None,
indent: Any = 1,
width: Any = 80,
depth: Any = None,
*,
compact: Any = False,
sort_dicts: Any = True,
underscore_numbers: Any = False) -> Any
>>> from pprint import pprint
>>>
>>> DATA = [
... ('firstname', 'lastname', 'age'),
... ('Alice', 'Apricot', 30),
... ('Bob', 'Blackthorn', 31),
... ('Carol', 'Corn', 32),
... ('Dave', 'Durian', 33),
... ('Eve', 'Elderberry', 34),
... ('Mallory', 'Melon', 15),
... ]
>>>
>>> pprint(DATA)
[('firstname', 'lastname', 'age'),
('Alice', 'Apricot', 30),
('Bob', 'Blackthorn', 31),
('Carol', 'Corn', 32),
('Dave', 'Durian', 33),
('Eve', 'Elderberry', 34),
('Mallory', 'Melon', 15)]
2.1.4. Pformat
Format a Python object into a pretty-printed representation.
def pformat(object: Any,
indent: Any = 1,
width: Any = 80,
depth: Any = None,
*,
compact: Any = False,
sort_dicts: Any = True,
underscore_numbers: Any = False) -> Any
>>> from pprint import pformat
>>>
>>> DATA = [
... ('firstname', 'lastname', 'age'),
... ('Alice', 'Apricot', 30),
... ('Bob', 'Blackthorn', 31),
... ('Carol', 'Corn', 32),
... ('Dave', 'Durian', 33),
... ('Eve', 'Elderberry', 34),
... ('Mallory', 'Melon', 15),
... ]
>>>
>>> result = pformat(DATA)
>>> result
"[('firstname', 'lastname', 'age'),\n ('Alice', 'Apricot', 30),\n ('Bob', 'Blackthorn', 31),\n ('Carol', 'Corn', 32),\n ('Dave', 'Durian', 33),\n ('Eve', 'Elderberry', 34),\n ('Mallory', 'Melon', 15)]"