zoukankan      html  css  js  c++  java
  • Python格式输出汇总

    print ('%10s'%('test'))
    print ('{:<10}'.format('test'))#left-aligned
    print ('{:>10}'.format('test'))#left-aligned
    print ('{:10}'.format('test'))# defaut(right-aligned)
    print ('{:_<10}'.format('test'))#padding character "_"
    print ('{:*<10}'.format('test'))#padding character "*"
    print ('{:^10}'.format('test'))#center-aligned
    print ('{:^10}'.format('ttest'))#center-aligned if uneven split
    
    # Truncating long strings
    print ('{:.5}'.format('xylophone'))
    # Combining truncating and padding
    print ('{:10.5}'.format('xylophone')) # truncating and padding
    # Numbers
    print ('{:d}'.format(42))
    print ('{:f}'.format(3.14159))
    # Padding numbers
    print ('{:4d}'.format(42))
    print ('{:04d}'.format(42))
    print ('{:06.2f}'.format(3.14159))
    print ('{:+d}'.format(42))
    # Use a space character to indicate that negative numbers should be
    # prefixed with a minus symbol and a leading space should be used
    # for positive ones
    print ('{: d}'.format(42))
    print ('{: d}'.format(-42))
    print ('{:=5d}'.format(-23))# control the position of the sign symbol
    print ('{:=5d}'.format(23))
    print ('{:=+5d}'.format(23))
    
    # Named placeholders
    
    data = {'first': 'Hodor', 'last':'Hodor!'}
    print ('{first} {last}'.format(**data))
    print ('{last} {first}'.format(**data))
    print ('{first} {last}'.format(first='Hodor', last='Hodor!'))
    
    person = {'first': 'Jean-Luc', 'last': 'Picard'}
    print (person['first']) # person[first] is NOT correct
    print ('{p[first]} {p[last]}'.format(p=person)) #p['first'] is NOT correct
    
    data = [10,20,30,40,50,60]
    print ('{d[4]} {d[5]}'.format(d=data))
    
    class Plant(object):
        type = 'tree'
    
    print ('{p.type}'.format(p=Plant()))
    
    class Plant(object):
    
        type = 'tree'
        kinds = [{'name':'oak'}, {'name':'maple'}]
    
    print ('{p.type}: {p.kinds[0][name]}'.format(p=Plant()))
    
    # Datatime
    
    from datetime import datetime as dt
    
    print (dt(2001,2,3,4,5))
    print ('{:%Y-%m-%d %H:%M}'.format(dt(2001,2,3,4,5)))
    
    # Parametrized formats
    
    print ('{:{align}{width}}'.format('test', align='^', width='10'))
    print ('{:{prec}} = {:.{prec}f}'.format('Gibberish', 2.7182, prec = 3))
    print ('{:{width}.{prec}f}'.format(2.7182, width = 5, prec =2))
    

    输出结果:

          test
    test      
          test
    test      
    test______
    test******
       test   
      ttest   
    xylop
    xylop     
    42
    3.141590
      42
    0042
    003.14
    +42
     42
    -42
    -  23
       23
    +  23
    Hodor Hodor!
    Hodor! Hodor
    Hodor Hodor!
    Jean-Luc
    Jean-Luc Picard
    50 60
    tree
    tree: oak
    2001-02-03 04:05:00
    2001-02-03 04:05
       test   
    Gibberish = 2.718
     2.72
    
  • 相关阅读:
    错误网络异常:android.os.NetworkOnMainThreadException
    方法服务android学习笔记42_服务的生命周期
    芯片软件随想录(关于核心技术)
    数组比特【编程珠玑】如何优化程序打印出小于100000的素数
    宋体函数Java乔晓松oracle分组函数
    调试客户端windbg远程调试方法
    方法对象Hibernate联合主键
    文件运行跟踪 root.sh 的 执行过程
    移植交叉编译pcDuino + busybox 成功搭建最小linux系统
    方法定制iOS学习笔记8UITableView的定制
  • 原文地址:https://www.cnblogs.com/yaos/p/7115804.html
Copyright © 2011-2022 走看看