格式化输出(%用法和format用法)

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

1 >>> print(‘%o‘ % 20)
2 24
3 >>> print(‘%d‘ % 20)
4 20
5 >>> print(‘%x‘ % 20)
6 14

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

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

(2)内置round()

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

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

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

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

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

 1 >>> print(‘%s‘ % ‘hello world‘)  # 字符串输出
 2 hello world
 3 >>> print(‘%20s‘ % ‘hello world‘)  # 右对齐,取20位,不够则补位
 4          hello world
 5 >>> print(‘%-20s‘ % ‘hello world‘)  # 左对齐,取20位,不够则补位
 6 hello world
 7 >>> print(‘%.2s‘ % ‘hello world‘)  # 取2位
 8 he
 9 >>> print(‘%10.2s‘ % ‘hello world‘)  # 右对齐,取2位
10         he
11 >>> print(‘%-10.2s‘ % ‘hello world‘)  # 左对齐,取2位
12 he        

4、 其他

字符串格式代码如下

(2)常用转义字符如下

二、format用法

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

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

1、基本用法

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

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

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

 1 >>> print(‘{} {}‘.format(‘hello‘,‘world‘))  # 不带字段
 2 hello world
 3 >>> print(‘{0} {1}‘.format(‘hello‘,‘world‘))  # 带数字编号
 4 hello world
 5 >>> print(‘{0} {1} {0}‘.format(‘hello‘,‘world‘))  # 打乱顺序
 6 hello world hello
 7 >>> print(‘{1} {1} {0}‘.format(‘hello‘,‘world‘))
 8 world world hello
 9 >>> print(‘{a} {tom} {a}‘.format(tom=‘hello‘,a=‘world‘))  # 带关键字
10 world hello world

2、进阶用法

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

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

 1 >>> print(‘{} and {}‘.format(‘hello‘,‘world‘))  # 默认左对齐
 2 hello and world
 3 >>> print(‘{:10s} and {:>10s}‘.format(‘hello‘,‘world‘))  # 取10位左对齐,取10位右对齐
 4 hello      and      world
 5 >>> print(‘{:^10s} and {:^10s}‘.format(‘hello‘,‘world‘))  # 取10位中间对齐
 6   hello    and   world
 7 >>> print(‘{} is {:.2f}‘.format(1.123,1.123))  # 取2位小数
 8 1.123 is 1.12
 9 >>> print(‘{0} is {0:>10.2f}‘.format(1.123))  # 取2位小数,右对齐,取10位
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‘)格式打印,值后面会有一个百分号。

 1 >>> print(‘{0:b}‘.format(3))
 2 11
 3 >>> print(‘{:c}‘.format(20))
 4
 5 >>> print(‘{:d}‘.format(20))
 6 20
 7 >>> print(‘{:o}‘.format(20))
 8 24
 9 >>> print(‘{:x}‘.format(20))
10 14
11 >>> print(‘{:e}‘.format(20))
12 2.000000e+01
13 >>> print(‘{:g}‘.format(20.1))
14 20.1
15 >>> print(‘{:f}‘.format(20))
16 20.000000
17 >>> print(‘{:n}‘.format(20))
18 20
19 >>> print(‘{:%}‘.format(20))
20 2000.000000%
21 >>> 

未完待续。。。

时间: 2024-08-29 18:38:54

格式化输出(%用法和format用法)的相关文章

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

%用法 1.整数的输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 >>> print('%o' % 20) 24 >>> print('%d' % 20) 20 >>> print('%x' % 20) 14 2.浮点数输出 2.1 格式化输出 %f ——保留小数点后面六位有效数字 %.3f,保留3位小数位%e ——保留小数点后面六位有效数字,指数形式输出 %.3e,保留3位小数位,使用科学计数法%g ——在保证

【Rollo的Python之路】Python:格式化输出:%与format

%用法: 1.0 整数的输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 >>> print('%o' % 20) 24 >>> print('%d' % 20) 20 >>> print('%x' % 20) 14 2.0 浮点数输出 %f 3.0 字符串输出 %d >>> print('%s' % 'hello world') # 字符串输出 hello world print("%

格式化输出%与format

一.%的用法 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 -

Java格式化输出

Java的格式化输出等同于String.Format,与C有很大的相似,比如 System.out.printf("%8.2f", x);在printf中,可以使用多个参数,例如: System.out.printf("Hello, %s. Next year, you'll be %d", name, age); 用于printf的转换符如下表: 转换符 类型 举例 d 十进制整数 159 x 十六进制整数 9f o 八进制整数 237 f 定点浮点数 15.9

java之格式化输出

参考http://how2j.cn/k/number-string/number-string-foramt/320.html#nowhere 格式化输出 如果不使用格式化输出,就需要进行字符串连接,如果变量比较多,拼接就会显得繁琐使用格式化输出,就可以简洁明了 %s 表示字符串%d 表示数字%n 表示换行 使用System.out.printf package digit; public class TestNumber { public static void main(String[] a

Python格式化输出——format用法示例

format OR % 提到Python中的格式化输出方法,一般来说有以下两种方式: print('hello %s' % 'world') # hello world print('hello {}'.format('world')) # hello world 到底哪种好呢,反正对我来说,用了.format()之后就再也不想用%了. format()不用理会数据类型,%s,%f等等我记不完: format()功能更丰富,填充方式,对齐方式都很灵活,让你的打印效果更美观: format()是官

格式化输出的几种方法 主要介绍format函数的用法

1 str自带函数格式化输出 rjust() ljust() center() zfill() zfill是补齐零 介绍: rjust(...) S.rjust(width[, fillchar]) -> string 给字符串格式化输出,对字符串本身不做处理,只是返回一个格式化过的字符串,可以指定字符串宽度,并且指定填充的字符,默认填充空格! 作用: 对齐输出 举例: In [26]: s1="1"         In [27]: s2="1234"   

Boost 的C++ 格式化输出函式库:Format

他最大的特色是在于它可以使用C 语言中printf 的格式化字串,来针对C++ 的iostream 做输出.或是产生格式化的字串:相较于C++ iostream 的manipulator,boost::format 在使用上更为直觉.简单. 而且和printf 不同的地方在于,他又有C++ iostream 的type safe.可以支持自定义类型的输出 官方网站的介绍可以参考: http://www.boost.org/doc/libs/1_44_0/libs/format/index.htm

浅谈format格式化输出

什么是format? 相对于基本格式化输出采用"%"的方法,format的功能强大,该函数把字符串当一个模板,通过传入的参数进行格式化,并且使用大括号"{}"作为特殊字符代替"%",有点类似C#里面的占位符 1)format的基本用法 不带编号,即"{}" 带数字编号,可调换顺序,即"{1}","{2}" 带关键字,即"{a}","{tom}" 例