1. print ('%10s'%('test'))
  2. print ('{:<10}'.format('test'))#left-aligned
  3. print ('{:>10}'.format('test'))#left-aligned
  4. print ('{:10}'.format('test'))# defaut(right-aligned)
  5. print ('{:_<10}'.format('test'))#padding character "_"
  6. print ('{:*<10}'.format('test'))#padding character "*"
  7. print ('{:^10}'.format('test'))#center-aligned
  8. print ('{:^10}'.format('ttest'))#center-aligned if uneven split
  9. # Truncating long strings
  10. print ('{:.5}'.format('xylophone'))
  11. # Combining truncating and padding
  12. print ('{:10.5}'.format('xylophone')) # truncating and padding
  13. # Numbers
  14. print ('{:d}'.format(42))
  15. print ('{:f}'.format(3.14159))
  16. # Padding numbers
  17. print ('{:4d}'.format(42))
  18. print ('{:04d}'.format(42))
  19. print ('{:06.2f}'.format(3.14159))
  20. print ('{:+d}'.format(42))
  21. # Use a space character to indicate that negative numbers should be
  22. # prefixed with a minus symbol and a leading space should be used
  23. # for positive ones
  24. print ('{: d}'.format(42))
  25. print ('{: d}'.format(-42))
  26. print ('{:=5d}'.format(-23))# control the position of the sign symbol
  27. print ('{:=5d}'.format(23))
  28. print ('{:=+5d}'.format(23))
  29. # Named placeholders
  30. data = {'first': 'Hodor', 'last':'Hodor!'}
  31. print ('{first} {last}'.format(**data))
  32. print ('{last} {first}'.format(**data))
  33. print ('{first} {last}'.format(first='Hodor', last='Hodor!'))
  34. person = {'first': 'Jean-Luc', 'last': 'Picard'}
  35. print (person['first']) # person[first] is NOT correct
  36. print ('{p[first]} {p[last]}'.format(p=person)) #p['first'] is NOT correct
  37. data = [10,20,30,40,50,60]
  38. print ('{d[4]} {d[5]}'.format(d=data))
  39. class Plant(object):
  40. type = 'tree'
  41. print ('{p.type}'.format(p=Plant()))
  42. class Plant(object):
  43. type = 'tree'
  44. kinds = [{'name':'oak'}, {'name':'maple'}]
  45. print ('{p.type}: {p.kinds[0][name]}'.format(p=Plant()))
  46. # Datatime
  47. from datetime import datetime as dt
  48. print (dt(2001,2,3,4,5))
  49. print ('{:%Y-%m-%d %H:%M}'.format(dt(2001,2,3,4,5)))
  50. # Parametrized formats
  51. print ('{:{align}{width}}'.format('test', align='^', width='10'))
  52. print ('{:{prec}} = {:.{prec}f}'.format('Gibberish', 2.7182, prec = 3))
  53. print ('{:{width}.{prec}f}'.format(2.7182, width = 5, prec =2))

输出结果:

  1. test
  2. test
  3. test
  4. test
  5. test______
  6. test******
  7. test
  8. ttest
  9. xylop
  10. xylop
  11. 42
  12. 3.141590
  13. 42
  14. 0042
  15. 003.14
  16. +42
  17. 42
  18. -42
  19. - 23
  20. 23
  21. + 23
  22. Hodor Hodor!
  23. Hodor! Hodor
  24. Hodor Hodor!
  25. Jean-Luc
  26. Jean-Luc Picard
  27. 50 60
  28. tree
  29. tree: oak
  30. 2001-02-03 04:05:00
  31. 2001-02-03 04:05
  32. test
  33. Gibberish = 2.718
  34. 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. 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 ...

  2. P1983车站分级

    %%%rqy 传送 我们注意到题目中这段话: 既然大于等于x的站都要停,那么不停的站的级别是不是都小于x?(这里讨论在始发站和终点站以内的站(注意这里是个坑)) 我们可以找出每趟车没停的站,向所有停了 ...

  3. machine learning 之 Recommender Systems

    整理自Andrew Ng的machine learning 课程 week 9. 目录: Problem Formulation(问题的形式) Content Based Recommendation ...

  4. 前端二倍图的思考(涉及Retina)

    EXCELL格式 1 csv格式导出来之后不能用EXCELL打开,会乱码.用记事本打开,然后将"(英文的引号出掉),就可以了. 关于二倍图的操作 概念: 设备像素:也叫物理像素,显示设备上最 ...

  5. 搭建maven本地仓库

    1. 需先配置java环境. 2. 下载nexus. https://www.sonatype.com/download-nexus-repo-oss?submissionGuid=a015a3db- ...

  6. ElasticSearch删除索引

    curl -X DELETE http://{ES IP address}:9200/{index_name}

  7. Kubernetes tutorial - K8S 官方入门教程 中文翻译

    官方教程,共 6 个小节.每一小节的第一部分是知识讲解,第二部分是在线测试环境的入口. kubectl 的命令手册 原文地址 1 创建集群 1.1 使用 Minikube 创建集群 Kubernete ...

  8. 【ABAP系列】SAP ABAP基础-录制BDC的MODE定义解析

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-录制BDC ...

  9. Apache Shiro简单介绍

    1. 概念 Apache Shiro 是一个开源安全框架,提供身份验证.授权.密码学和会话管理.Shiro 框架具有直观.易用等特性,同时也能提供健壮的安全性,虽然它的功能不如 SpringSecur ...

  10. jQuery DataTables 问题:Cannot reinitialise DataTable

    解决:  destroy: true, var tabel = $('#userlist').DataTable({        destroy: true,                    ...