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

Python格式输出汇总的更多相关文章

  1. Python——格式输出,基本数据

    一.问题点(有待解决) 1.Python中只有浮点数,20和20.0是否一样? from decimal import Decimal  a = Decimal('1.3') round() 参考文章 ...

  2. PAT 1006 换个格式输出整数 (15)(C++&JAVA&Python)

    1006 换个格式输出整数 (15)(15 分) 让我们用字母B来表示"百".字母S表示"十",用"12...n"来表示个位数字n(& ...

  3. python基础入门--input标签、变量、数字类型、列表、字符串、字典、索引值、bool值、占位符格式输出

    # 在python3 中: # nian=input('>>:') #请输入什么类型的值,都成字符串类型# print(type(nian)) # a = 2**64# print(typ ...

  4. 孤荷凌寒自学python第九天Python的输出print的格式化

    孤荷凌寒自学python第九天Python的输出print的格式化 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) (今天感觉手写笔记整得清楚些,汇总电子 笔记时,自己思路凌乱了,练习过程也还 ...

  5. Python日志输出——logging模块

    Python日志输出——logging模块 标签: loggingpythonimportmodulelog4j 2012-03-06 00:18 31605人阅读 评论(8) 收藏 举报 分类: P ...

  6. python格式化输出【转】

    今天写代码时,需要统一化输出格式进行,一时想不起具体细节,用了最笨的方法,现在讲常见的方法进行一个总结. 一.格式化输出 1.整数的输出 直接使用'%d'代替可输入十进制数字: >>> ...

  7. python高亮显示输出

    知识内容: 1.高亮输出语法 2.高亮输出实例 前言: 在做购物车这道题时遇到了高亮显示输出某些内容的需求,于是就学了一下这方面的知识,以下是python高亮显示输出的使用方法: 购物车链接:  ht ...

  8. Python 日志输出中添加上下文信息

    Python日志输出中添加上下文信息 除了传递给日志记录函数的参数(如msg)外,有时候我们还想在日志输出中包含一些额外的上下文信息.比如,在一个网络应用中,可能希望在日志中记录客户端的特定信息,如: ...

  9. Python格式化输出的三种方式

    Python格式化输出的三种方式 一.占位符 程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式比如要求用户输入用户名和年龄,然后打印如下格式:My name is xxx,my age ...

随机推荐

  1. 笨办法学Python(learn python the hard way)--练习程序11-20

    #ex11.py1 print("How old are you?",end=''), age = input() print("How tall are you?&qu ...

  2. Python 装饰器之 functools.wraps

    在看 Bottle 代码中看见 functools.wraps 这种用法. def make_default_app_wrapper(name): """ Return ...

  3. CDN:BootCDN 项目列表-摘录-20180405

    ylbtech-CDN:BootCDN 项目列表-20180405 1.返回顶部 1.   2. 2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. http://www.boo ...

  4. ajax跨域处理 No 'Access-Control-Allow-Origin' header is present on the requested resource 问题

    Controller层的类上增加@CrossOrign注解,当前文件的所有接口就都可以被调用 spring注解@CrossOrigin不起作用的原因 1.是springMVC的版本要在4.2或以上版本 ...

  5. WPF属性之理解附加属性

    附加属性,顾名思义,和被附加的控件没有依赖关系,只是强行给目标控件挂上一个“属性值”,以便于操作之.就好比,你在学校是学生,那么就要听老师的管教,在公司是下属,就要服从老板的命令一样. 我们常见的附加 ...

  6. Vue作用域插槽:用作循环结构的模版

    一 项目结构 二 App组件 <template> <div id="app"> <!-- 子组件 --> <todos :list=&q ...

  7. Mac004--Tomcat安装

    Mac--Tomcat安装 一.Tomcat下载 https://tomcat.apache.org/download-80.cgi 下载Tomcat注意tomcat与jdk版本要一致.jdk1.8版 ...

  8. 洛谷 P1546 最短网络 Agri-Net(最小生成树)

    题目链接 https://www.luogu.org/problemnew/show/P1546 说过了不复制内容了 显然是个最小生成树. 解题思路 prim算法 Kruskal算法 prim算法很直 ...

  9. 物流运输(最短路+dp)

    这道题是相当的火,但是在tyher的讲解下我一遍就AC了!!! Part 1 理解题目 从第一天到最后一天,总会有一些点莫名其妙地走不了,所以导致我们不能按照上一次的最短路一直运输得到最少费用,而需要 ...

  10. python爬取企业登记业务

    import requests from lxml import etree import csv for i in range(10, 990, 10): url = "http://12 ...