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
Python格式输出汇总的更多相关文章
- Python——格式输出,基本数据
一.问题点(有待解决) 1.Python中只有浮点数,20和20.0是否一样? from decimal import Decimal a = Decimal('1.3') round() 参考文章 ...
- PAT 1006 换个格式输出整数 (15)(C++&JAVA&Python)
1006 换个格式输出整数 (15)(15 分) 让我们用字母B来表示"百".字母S表示"十",用"12...n"来表示个位数字n(& ...
- python基础入门--input标签、变量、数字类型、列表、字符串、字典、索引值、bool值、占位符格式输出
# 在python3 中: # nian=input('>>:') #请输入什么类型的值,都成字符串类型# print(type(nian)) # a = 2**64# print(typ ...
- 孤荷凌寒自学python第九天Python的输出print的格式化
孤荷凌寒自学python第九天Python的输出print的格式化 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) (今天感觉手写笔记整得清楚些,汇总电子 笔记时,自己思路凌乱了,练习过程也还 ...
- Python日志输出——logging模块
Python日志输出——logging模块 标签: loggingpythonimportmodulelog4j 2012-03-06 00:18 31605人阅读 评论(8) 收藏 举报 分类: P ...
- python格式化输出【转】
今天写代码时,需要统一化输出格式进行,一时想不起具体细节,用了最笨的方法,现在讲常见的方法进行一个总结. 一.格式化输出 1.整数的输出 直接使用'%d'代替可输入十进制数字: >>> ...
- python高亮显示输出
知识内容: 1.高亮输出语法 2.高亮输出实例 前言: 在做购物车这道题时遇到了高亮显示输出某些内容的需求,于是就学了一下这方面的知识,以下是python高亮显示输出的使用方法: 购物车链接: ht ...
- Python 日志输出中添加上下文信息
Python日志输出中添加上下文信息 除了传递给日志记录函数的参数(如msg)外,有时候我们还想在日志输出中包含一些额外的上下文信息.比如,在一个网络应用中,可能希望在日志中记录客户端的特定信息,如: ...
- Python格式化输出的三种方式
Python格式化输出的三种方式 一.占位符 程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式比如要求用户输入用户名和年龄,然后打印如下格式:My name is xxx,my age ...
随机推荐
- Why is HttpContext.Current null during the Session_End event?
Why is HttpContext.Current null during the Session_End event? On Session_End there is no communicati ...
- P1983车站分级
%%%rqy 传送 我们注意到题目中这段话: 既然大于等于x的站都要停,那么不停的站的级别是不是都小于x?(这里讨论在始发站和终点站以内的站(注意这里是个坑)) 我们可以找出每趟车没停的站,向所有停了 ...
- machine learning 之 Recommender Systems
整理自Andrew Ng的machine learning 课程 week 9. 目录: Problem Formulation(问题的形式) Content Based Recommendation ...
- 前端二倍图的思考(涉及Retina)
EXCELL格式 1 csv格式导出来之后不能用EXCELL打开,会乱码.用记事本打开,然后将"(英文的引号出掉),就可以了. 关于二倍图的操作 概念: 设备像素:也叫物理像素,显示设备上最 ...
- 搭建maven本地仓库
1. 需先配置java环境. 2. 下载nexus. https://www.sonatype.com/download-nexus-repo-oss?submissionGuid=a015a3db- ...
- ElasticSearch删除索引
curl -X DELETE http://{ES IP address}:9200/{index_name}
- Kubernetes tutorial - K8S 官方入门教程 中文翻译
官方教程,共 6 个小节.每一小节的第一部分是知识讲解,第二部分是在线测试环境的入口. kubectl 的命令手册 原文地址 1 创建集群 1.1 使用 Minikube 创建集群 Kubernete ...
- 【ABAP系列】SAP ABAP基础-录制BDC的MODE定义解析
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-录制BDC ...
- Apache Shiro简单介绍
1. 概念 Apache Shiro 是一个开源安全框架,提供身份验证.授权.密码学和会话管理.Shiro 框架具有直观.易用等特性,同时也能提供健壮的安全性,虽然它的功能不如 SpringSecur ...
- jQuery DataTables 问题:Cannot reinitialise DataTable
解决: destroy: true, var tabel = $('#userlist').DataTable({ destroy: true, ...