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 ...
随机推荐
- TabController定义顶部tab切换
前面通过DefaultTabController组件实现了AppBar里面的顶部导航切换,但是在项目中有数据请求,上拉加载更多等操作的时候,前面的写法,就不是很方便操作,因此,在flutter里面,还 ...
- 晋江年下文爬取【xpath】
''' @Modify Time @Author 目标:晋江年下文 爬取6页 ------------ ------- http://www.jjwxc.net/search.php?kw=%C4%E ...
- ModuleNotFoundError: No module named 'mysql'
问题:使用sqlalchemy初始化数据库报错,如图: 解决办法: pip install mysql-connector 安装后即可正常运行了 参照: https://blog.csdn.net/j ...
- spring4.1.8扩展实战之五:改变bean的定义(BeanFactoryPostProcessor接口)
本章我们继续实战spring的扩展能力,通过自定义BeanFactoryPostProcessor接口的实现类,来对bean实例做一些控制: 原文地址:https://blog.csdn.net/bo ...
- Netty精进01
为什么要学习Netty? 目前基于Netty实现的一些优秀的开源框架:Dubbo.RocketMQ.Spark.Spring5.Flink.ElasticSearch.gRPC……这些还说明不了为什么 ...
- centos 7 环境准备工作
删除自带jdk: rpm -e --nodeps `rpm -qa | grep java` 打开 /etc/ 目录下的 profile 文件: vi /etc/profile 将如下代码追加到 pr ...
- python学习笔记:__init__.py的作用
package标识,而非文件夹. 在pycharm中new,有Directory和Python Package两个选项. 分别创建Directory和package,发现前者只是一个空目录,后者包含一 ...
- C#后台获取当前时间并格式化
最常用的:DateTime.Now.ToString("yyyy-MM-dd"); //今天DateTime.Now.Date.ToShortDateString(); //昨天, ...
- mysql 5.7 创建函数报错,This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creat
今天用命令创建函数, 报错 This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration ...
- eclipsePreferences位置
1.Windows:菜单栏-Window-Preferences 2.Mac:应用顶部最左侧Eclipse-Preferences ---------------------------------- ...