转自:https://www.cnblogs.com/fat39/p/7159881.html

一、格式化输出
1、整数的输出
%o —— oct 八进制
%d —— dec 十进制
%x —— hex 十六进制

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

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

 >>> print('%f' % 1.11)  # 默认保留6位小数
1.110000
>>> print('%.1f' % 1.11) # 取1位小数
1.1
>>> print('%e' % 1.11) # 默认6位小数,用科学计数法
1.110000e+00
>>> print('%.3e' % 1.11) # 取3位小数,用科学计数法
1.110e+00
>>> print('%g' % 1111.1111) # 默认6位有效数字
1111.11
>>> print('%.7g' % 1111.1111) # 取7位有效数字
1111.111
>>> print('%.2g' % 1111.1111) # 取2位有效数字,自动转换为科学计数法
1.1e+03

(2)内置round()

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

round()函数只有一个参数,不指定位数的时候,返回一个整数,而且是最靠近的整数,类似于四舍五入,当指定取舍的小数点位数的时候,一般情况也是使用四舍五入的规则,但是碰到.5的情况时,如果要取舍的位数前的小数是奇数,则直接舍弃,如果是偶数则向上取舍。

注:“.5”这个是一个“坑”,且python2和python3出来的接口有时候是不一样的,尽量避免使用round()函数吧

 >>> round(1.1125)  # 四舍五入,不指定位数,取整
1
>>> round(1.1135,3) # 取3位小数,由于3为奇数,则向下“舍”
1.113
>>> round(1.1125,3) # 取3位小数,由于2为偶数,则向上“入”
1.113
>>> round(1.5) # 无法理解,查阅一些资料是说python会对数据进行截断,没有深究
2
>>> round(2.5) # 无法理解
2
>>> round(1.675,2) # 无法理解
1.68
>>> round(2.675,2) # 无法理解
2.67
>>>

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

 >>> print('%s' % 'hello world')  # 字符串输出
hello world
>>> print('%20s' % 'hello world') # 右对齐,取20位,不够则补位
hello world
>>> print('%-20s' % 'hello world') # 左对齐,取20位,不够则补位
hello world
>>> print('%.2s' % 'hello world') # 取2位
he
>>> print('%10.2s' % 'hello world') # 右对齐,取2位
he
>>> print('%-10.2s' % 'hello world') # 左对齐,取2位
he

4、 其他

字符串格式代码如下

(2)常用转义字符如下

二、format用法

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

使用方法由两种:b.format(a)和format(a,b)。

1、基本用法

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

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

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

 >>> print('{} {}'.format('hello','world'))  # 不带字段
hello world
>>> print('{0} {1}'.format('hello','world')) # 带数字编号
hello world
>>> print('{0} {1} {0}'.format('hello','world')) # 打乱顺序
hello world hello
>>> print('{1} {1} {0}'.format('hello','world'))
world world hello
>>> print('{a} {tom} {a}'.format(tom='hello',a='world')) # 带关键字
world hello world

2、进阶用法

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

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

 >>> print('{} and {}'.format('hello','world'))  # 默认左对齐
hello and world
>>> print('{:10s} and {:>10s}'.format('hello','world')) # 取10位左对齐,取10位右对齐
hello and world
>>> print('{:^10s} and {:^10s}'.format('hello','world')) # 取10位中间对齐
hello and world
>>> print('{} is {:.2f}'.format(1.123,1.123)) # 取2位小数
1.123 is 1.12
>>> print('{0} is {0:>10.2f}'.format(1.123)) # 取2位小数,右对齐,取10位
1.123 is 1.12

3、多个格式化

'b' - 二进制。将数字以2为基数进行输出。
'c' - 字符。在打印之前将整数转换成对应的Unicode字符串。
'd' - 十进制整数。将数字以10为基数进行输出。
'o' - 八进制。将数字以8为基数进行输出。
'x' - 十六进制。将数字以16为基数进行输出,9以上的位数用小写字母。
'e' - 幂符号。用科学计数法打印数字。用'e'表示幂。
'g' - 一般格式。将数值以fixed-point格式输出。当数值特别大的时候,用幂形式打印。
'n' - 数字。当值为整数时和'd'相同,值为浮点数时和'g'相同。不同的是它会根据区域设置插入数字分隔符。
'%' - 百分数。将数值乘以100然后以fixed-point('f')格式打印,值后面会有一个百分号。
 >>> print('{0:b}'.format(3))
11
>>> print('{:c}'.format(20)) >>> print('{:d}'.format(20))
20
>>> print('{:o}'.format(20))
24
>>> print('{:x}'.format(20))
14
>>> print('{:e}'.format(20))
2.000000e+01
>>> print('{:g}'.format(20.1))
20.1
>>> print('{:f}'.format(20))
20.000000
>>> print('{:n}'.format(20))
20
>>> print('{:%}'.format(20))
2000.000000%
>>>

【Python笔记】1、格式化输出(%用法和format用法)的更多相关文章

  1. 格式化输出(%用法和fomat用法)

    一:%用法 1.整数输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 2.浮点数输出 %f ——保留小数点后面六位有效数字 %.3f,保留3位小数位%e ——保留 ...

  2. python基础_格式化输出(%用法和format用法)(转载)

    python基础_格式化输出(%用法和format用法) 目录 %用法 format用法 %用法 1.整数的输出 %o -- oct 八进制%d -- dec 十进制%x -- hex 十六进制 &g ...

  3. Python 3.x 格式化输出字符串 % & format 笔记

    Python 3.x 格式化输出字符串 % & format 笔记 python格式化字符串有%和{}两种 字符串格式控制符. 字符串输入数据格式类型(%格式操作符号) %%百分号标记 %c字 ...

  4. python 格式化输出(% VS format)

    提到Python中的格式化输出方法,一般来说有以下两种方式: 1)% 格式说明由%和格式字符组成,如%f,%s,%d,它的作用是将数据按照指定的格式输出.格式说明是由“%”字符开始的. #1.输出字符 ...

  5. python - 字符串的格式化输出

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_2_str.py@ide: PyCharm Community ...

  6. Python 入门之格式化输出

    Python 入门之格式化输出 1.格式化 (1)%为占位 (2)%s --- 站字符串的位置(数字.字符串都能够进行填充) name = input('请输入姓名:') age = input('请 ...

  7. Python基础篇(格式化输出,运算符,编码):

    Python基础篇(格式化输出,运算符,编码): 格式化输出: 格式:print ( " 内容%s" %(变量)) 字符类型: %s  替换字符串      %d 替换整体数字  ...

  8. python基础_格式化输出(%用法和format用法)

      目录 %用法 format用法 %用法 1.整数的输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 1 >>> print('%o' % 2 ...

  9. python学习笔记 | strftime()格式化输出时间

    time模块 import time t = time.strftime("%Y-%m-%d %H:%M:%S") print(t) datetime模块 import datet ...

随机推荐

  1. vim:spell语法

    先说结论,在vim配置文件加入: setlocal spell spelllang=en_us,cjk 1.spell指开启检查模式. 2.spelllang用于指定检查的种类. 3.cjk,指中国, ...

  2. Docker Cheatsheet

    一.创建 docker create:创建容器,处于停止状态. centos:latest:centos容器:最新版本(也可以指定具体的版本号).本地有就使用本地镜像,没有则从远程镜像库拉取.创建成功 ...

  3. 上传文件至Github仓库

    Github的使用 创建一个本地的版本库(也就是一个文件夹) 新建文件夹,或使用Git bash命令行窗口创建 $ mkdir test $ cd test 在命令行中输入 git init把这个文件 ...

  4. S5PV210 固件烧写 u-boot烧写

    首先阅读CW210_CD自带光盘中CW210 开发板使用手册.pdf 使用usb 拨码开关置成usb启动.xx可以是ON或OFF.开发板上面也有丝印提示 usb线接好,串口线接好 使用DNW下载 自带 ...

  5. rf中setup与teardown

    setup:是测试一个用例(或者套件)前要做的事情 teardown:是测试后要做的事情 在RF中,每个测试套件目录.测试套件文件.测试用例 都可以有自己的setup 和teardown 所有的 se ...

  6. postgresql9.5编译安装体验

    实验环境: centos7.6 pgsql9.5 源码编译安装 实验目的: 体验源码编译安装pgsql 01.download https://ftp.postgresql.org/pub/sourc ...

  7. rest framework 之序列化

    一.示例 restful work 的序列号就类似于 Django 的 Form 表单. 1.api/urls.py from django.urls import path, re_path fro ...

  8. Mac开源文本编辑器Brackets 快捷操作有哪些?

    Brackets for Mac是一款可靠,直观且易于使用的macOS应用程序,可用作HTML,CSS和JavaScript项目的代码编辑器.如果你能了解更多Mac开源文本编辑器Brackets 快捷 ...

  9. Hibernate各种状态(瞬时状态、持久化状态、游离|托管状态)之间的转换

  10. pandas用法总结

    pandas用法总结 2018年06月07日 10:49:03 一夜了 阅读数 38705更多 分类专栏: 杂项   一.生成数据表 1.首先导入pandas库,一般都会用到numpy库,所以我们先导 ...