Make Python output more beautiful---pprint

Refer to "Python3 from entry to actual combat"

	from pprint import pprint
    
    #sep输出项之间的分隔符
    print("许嵩","薛之谦","林俊杰",sep="  and  ")
    
    #改变print的默认换行操作:end
    print("啦","啦","啦",end=" + = ")
    
    #换行 这里没有换行的话 字就挤到一行去了
    print("")
    
    #格式化输出
    #%d整数 %f浮点数 %s字符串 %p数据的内存地址(十六进制)
    name = "wang"
    score = 56.358
    #.2f是保留二位小数
    print("学生%s的分数是%.2f"%(name,score))
    
    #通过{}和:来代替
    name = "wang"
    score = 56.358
    #.2f是保留二位小数
    print("学生{}的分数是{:.2f}".format(name,score))
    
    #str.rjust() str.ljust() str.center() 
    #控制字符串输出的宽度和字符串的对齐方式 (靠右靠左靠中间)
    s = 'hi'
    t = 'the'
    print(s.rjust(5),t.rjust(10))
    print(s.ljust(5),t.ljust(10))
    print(s.center(5),t.center(10))
    输出结果:
    许嵩  and  薛之谦  and  林俊杰
	啦 啦 啦 + = 
	学生wang的分数是56.36
	学生wang的分数是56.36
	   hi        the
	hi    the       
	  hi     the    
#pprint 该函数以单行格式输出对象,如果不符合允许的宽度,则将对象分成多行.
    data = [
        (1,{
    
    'a':'A','b':'B','c':'C'}),
         (2,{
    
    'hello',3.14}),
        [[1,2,3],[4,5,6],[7,8,9],{
    
    'h','j','k'}]]
    
    print(data)
    print("*"*100)#注意进行对比
    pprint(data)
    print("*"*100)
	#不希望输出所有细节的数据
	pprint(data,depth=2)
	#width控制宽度,indent调整缩进
	pprint(data,width=80,indent=4)
	运行结果如下:
	[(1, {
    
    'a': 'A', 'b': 'B', 'c': 'C'}), (2, {
    
    3.14, 'hello'}), [[1, 2, 3], [4, 5, 6], [7, 8, 9], {
    
    'k', 'j', 'h'}]]
	****************************************************************************************************
	[(1, {
    
    'a': 'A', 'b': 'B', 'c': 'C'}),
	 (2, {
    
    3.14, 'hello'}),
	 [[1, 2, 3], [4, 5, 6], [7, 8, 9], {
    
    'k', 'j', 'h'}]]
	****************************************************************************************************
	[(1, {
    
    ...}), (2, {
    
    3.14, 'hello'}), [[...], [...], [...], {
    
    'k', 'j', 'h'}]]
	[   (1, {
    
    'a': 'A', 'b': 'B', 'c': 'C'}),
	    (2, {
    
    3.14, 'hello'}),
	    [[1, 2, 3], [4, 5, 6], [7, 8, 9], {
    
    'k', 'j', 'h'}]]

Guess you like

Origin blog.csdn.net/qq_45911278/article/details/111656782