1 %用法

1.1整数的输出

%o —— oct 八进制
%d —— dec 十进制
%x —— hex 十六进制

print('%o' % 20)  #24
print('%d' % 20)  #20
print('%x' % 20)  #14

1.2浮点数输出

%f ——默认保留小数点后面六位有效数字
  %.3f,保留3位小数位
%e ——默认保留小数点后面六位有效数字,指数形式输出
  %.3e,保留3位小数位,使用科学计数法
%g ——在保证六位有效数字的前提下,使用小数方式,否则使用科学计数法
  %.3g,保留3位有效数字,使用小数或科学计数法

1.3round四舍五入

round(number[, ndigits])
参数:
number - 这是一个数字表达式。
ndigits - 表示从小数点到最后四舍五入的位数。默认值为0。
返回值
该方法返回x的小数点舍入为n位数后的值。

print(round(1.1125))     #1
print(round(1.1135,3))  #1.113
print(round(1.1125,3))  #1.113

1.4字符串输出

%s
%10s——右对齐,占位符10位
%-10s——左对齐,占位符10位
%.2s——截取2位字符串
%10.2s——10位占位符,截取两位字符串

print('%s' % 'hello world')
print('%s' % 1)
print('%20s' % 'hello world')
print('%-20s' % 'hello world')
print('%.2s' % 'hello world')
print('%10.2s' % 'hello world')
print('%-10.2s' % 'hello world')

输出结果:

print(’%s’ % ‘hello world’)
print(’%s’ % 1)
print(’%20s’ % ‘hello world’)
print(’%-20s’ % ‘hello world’)
print(’%.2s’ % ‘hello world’)
print(’%10.2s’ % ‘hello world’)
print(’%-10.2s’ % ‘hello world’)

常见格式:

常见转义字符:

2 format用法

format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号‘{}’作为特殊字符代替‘%’

2.1位置匹配

(1)不带编号,即“{}”

(2)带数字编号,可调换顺序,即“{1}”、“{2}”

(3)带关键字,即“{a}”、“{tom}”

print('{} {}'.format('hello','world'))
print('{0} {1}'.format('hello', 'world'))
print('{0} {1} {0}'.format('hello','world'))
print('{1} {1} {0}'.format('hello','world'))
print('{a} {tom} {a}'.format(tom='hello',a='world'))

输出结果

hello world
hello world
hello world hello
world world hello
world hello world

print( '{0}, {1}, {2}'.format('a', 'b', 'c'))
print('{}, {}, {}'.format('a', 'b', 'c'))
print( '{2}, {1}, {0}'.format('a', 'b', 'c'))
print('{2}, {1}, {0}'.format(*'abc'))  # 可打乱顺序
print('{0}{1}{0}'.format('abra', 'cad'))  # 可重复

输出结果:

a, b, c
a, b, c
c, b, a
c, b, a
abracadabra

import datetime
d=datetime.datetime(2010,7,4,12,15,34)
print('{:%Y-%m-%d %H:%M:%S}'.format(d))

输出结果:

2010-07-04 12:15:34

2.2左中右对齐及位数补全

(1)< (默认)左对齐、> 右对齐、^ 中间对齐、= (只用于数字)在小数点后进行补齐

(2)取位数“{:4s}”、"{:.2f}"等

points=19
total=22
print('Correct answers: {:.2%}'.format(points/total))
print('{} is {:.2f}'.format(1.123,1.123))
print('{0} is {0:>10.2f}'.format(1.123))
print('{:+f}; {:+f}'.format(3.14, -3.14))  # 总是显示符号
print('{:,}'.format(1234567890))

输出结果:

Correct answers: 86.36%
1.123 is 1.12
1.123 is 1.12
+3.140000; -3.140000
1,234,567,890

python 格式化输出%和format相关推荐

  1. Python格式化输出之format

    format用法 相对基本格式化输出采用'%'的方法,format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号'{}'作为特殊字符代替'%' 使用方法由两种:b ...

  2. python格式化输出以及format()函数

    1.格式化输出 # 根据我们用户的需求,在指定的位置输出内容 """ %s 格式化输出字符串 str %d 格式化输出有符号的整数 %f 格式化输出浮点数 "& ...

  3. python格式化输出之format用法

    这篇博客总结不错 https://www.cnblogs.com/lovejh/p/9201219.html

  4. python基础语法手册format-python的格式化输出(format,%)实例详解

    皇城PK Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了format这个格式化字符串的方法之后,我认为%还是format这根本就不算个问 ...

  5. Python格式化输出(format和%)

    Python格式化输出(format和%) 平时在打印结果的时候,直接就使用print,但多次使用print会导致界面看起来很乱.python提供两种格式化输出的方法--format和%,本文会介绍两 ...

  6. Python格式化输出方法

    Python格式化输出 本文转自:Python格式化输出 今天写程序又记不清格式化输出细节了--= =索性整理一下. python print格式化输出. 1. 打印字符串 print (" ...

  7. Python 格式化输出 —— 小数转化为百分数

    比如将 0.1234 转化为 12.34% 的形式: rate = .1234 print('%.2f%%' % (rate * 100)) 第一个百分号和 .2f 相连,表示浮点数类型保留小数点后两 ...

  8. python格式化输出

    python格式化输出 格式化输出规范1(, + ): print( ) 打印输出函数是在开发中用得很多的函数,代表输出并换行. 1.print(字符常量 + 字符变量) 说明:+ 加号仅用于连接两个 ...

  9. python格式化输出(二)--字符串的格式化输出

    ** ​字符串的格式化输出 ** 1.使用占位符 (1)三个常用占位符用法 ①d:将整数.浮点数转换成十进制表示.取整,不进行四舍五入. ②f:将整数.浮点数转换成浮点数表示,默认保留小数点后6位,四 ...

最新文章

  1. CCNET+MSBuild+SVN实时构建的优化总结
  2. 导出勾选密码永不过期的AD账户信息
  3. 使用SD-WAN进行WAN转换的业务影响—Vecloud微云
  4. java域的控制修饰符可分为_Java中的类和方法的修饰符
  5. 现代谱估计:多窗口谱分析和显著性
  6. 安全编程: 防止缓冲区溢出
  7. Android开发之java8 lambad表达式的使用
  8. Linux命令【四】文件+虚拟内存+常用系统函数
  9. 腾讯翻译君在线翻译怎么翻译整个文件_Word文档翻译:分享下面几种方法
  10. APKTOOL反编译使用教程
  11. 原生ajax上传获取进度,ajax上传图片获取进度
  12. 批量将多个 txt 记事本文件合并成一个独立的记事本文件
  13. PTC Creo 8最新版下载
  14. P1567 统计天数【入门】
  15. 没错,我在用JBOD,它真的很好
  16. 【Python】WARNING: The script xx.exe is installed in xxdirectory which is not on PATH.
  17. 裸奔系列之博科SAN交换机(3)---SAN交换机初始化
  18. matlab滤波有几种形式,几种经典常用的滤波算法
  19. 数据库中的CAP原理
  20. 请问如何修复损坏的jpg文件

热门文章

  1. java 的构造函数修饰符public private protected
  2. apache2 php mysql_二、Linux服务器apache2+PHP7+mysql环境配置
  3. 详解 ConcurrentHashMap
  4. 淮北市成人学计算机学校,安徽淮北市成人学电脑?
  5. thinkphp三级分销小程序源码_山东谷道微信小程序商城源码带后台 公众号平台三级分销系统...
  6. curl在android服务器上编译,Android curl的上载编译和使用
  7. dll模块化设计与编程_FPGA设计原则经验分享
  8. app名字变为android+api,一起来做个app吧 wanandroid开放API
  9. ROS配置分布式通信
  10. linux中split函数用法,Linux csplit 命令用法详解-Linux命令大全(手册)