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 ...
随机推荐
- JS判断数组是否为空
var arr = []; if(arr == false){ console.log("arr is empty!"); }else{ console.log("arr ...
- HDU | 1874 畅通工程续 SPFA&DIJIESITLA
题目: 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰 ...
- sql-hive笔试题整理 1 (学生表-成绩表-课程表-教师表)
题记:一直在写各种sql查询语句,最长的有一百多行,自信什么需求都可以接,可......,想了想,可能一直在固定的场景下写,平时也是以满足实际需求为目的,竟不知道应试的题都是怎么出的,又应该怎么做.遂 ...
- CSU 1553 Good subsequence(RMQ问题 + 二分)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1553 Description Give you a sequence of n nu ...
- linux系统忘记root密码的解决办法--(超细致讲解!)
在本博客中我采用虚拟机和radhet6.5作为示范: 首先:重新系统系统,在系统重新启动是未进入系统检查之前按下"e"键: 一定要在倒计时到0秒之前按下"e", ...
- crazyflie四轴飞行器
源地址:http://www.bitcraze.se/2013/02/pre-order-has-started/ Crazyflie是一个开源的纳米四旋翼 来几张靓照 开发平台是开源的,所以原理图和 ...
- (appium+python)UI自动化_02_appium启动手机app
前提:需先安装配置好appium+python自动化环境,已配置好环境的小伙伴可以参考以下步骤启动Android app,具体步骤如下: 一.USB连接手机 (1)手机USB连接电脑 (2)手机打开开 ...
- 【洛谷p1025】数的划分
数的划分[传送门] 算法的话,dfs+剪枝: 据说是01年之前的NOIp提高组: 思路: 这道题是求把n无序的划分成k份的方案数,最直接的搜索方法是依次枚举x1,x2……xk的值,然后判断,显然这么搜 ...
- dp(最长升序列)
http://poj.org/problem?id=2533 题意:给你n(1-1000)个数,求这n个数的最长升序列. 题解:dp[i]表示以第i个数结尾的最长升序列. #include & ...
- 选择指定的MySQL数据库
<?php /******************************** *** 功能:选择指定的MySQL数据库 *********************************/ ? ...