Do you know the difference between print and pprint?

Do you know the difference between print and pprint?

Both print() and pprint() are python printing modules. The functions are basically the same. The only difference is that the data structure printed by the pprint() module is more complete. Each row has a data structure, which makes it easier to read the printed output results. Especially for very long data printing, the output results of print() are all on one line, which is not convenient to view, while pprint() uses line-by-line printing. Therefore, for data with complex data structures and long data lengths, pprint() is suitable printing method. Of course, print() is generally used in most cases.

The following is a code example:

import pprint

data = ("test", [1, 2, 3,'test', 4, 5], "This is a string!",
        {
    
    'age':23, 'gender':'F'})
print(data)
pprint.pprint(data)

Output result:

('test',
 [1, 2, 3, 'test', 4, 5],
 'This is a string!',
 {
    
    'age': 23, 'gender': 'F'})

Guess you like

Origin blog.csdn.net/m0_45388819/article/details/110098017