“%”的使用

格式符

描述

%s

字符串 (采用str()的显示)

%r

字符串 (采用repr()的显示)

%c

单个字符及其ASCII码

%u

整数(无符号)

%b

二进制整数

%o

八进制数(无符号)

%d

十进制整数

%i

十进制整数

%x

十六进制数(无符号)

%X

十六进制数大写(无符号)

%e

指数 (基底写为e),用科学计数法格式化浮点数

%E

指数 (基底写为E),作用同%e

%f

浮点数,可指定小数点后的精度

%g

%f和%e的简写,指数(e)或浮点数 (根据显示长度)

%G

%F和%E的简写,指数(E)或浮点数 (根据显示长度)

%p

用十六进制数格式化变量的地址

%%

转义,字符"%"

字符串输出(%s)

%10s——右对齐,占位符10位

%-10s——左对齐,占位符10位

%.2s——截取2位字符串

%10.2s——10位占位符,截取两位字符串

# 字符串输出

print('%s' % 'hello world') # 结果:hello world

# 右对齐,取20位,不够则补位

print('%20s' % 'hello world') # 结果: hello world

# 左对齐,取20位,不够则补位

print('%-20s' % 'hello world') # 结果:hello world

# 取2位

print('%.2s' % 'hello world') # 结果:he

# 右对齐,占位符10位,取2位

print('%10.2s' % 'hello world') # 结果: he

# 左对齐,占位符10位,取2位

print('%-10.2s' % 'hello world') # 结果:he

浮点数输出(%f)

%f ——保留小数点后面六位有效数字

%.3f,保留3位小数位

%e ——保留小数点后面六位有效数字,指数形式输出

%.3e,保留3位小数位,使用科学计数法

%g ——在保证六位有效数字的前提下,使用小数方式,否则使用科学计数法

%.3g,保留3位有效数字,使用小数或科学计数法

# 默认保留6位小数

print('%f' % 1.11) # 1.110000

# 取1位小数

print('%.1f' % 1.11) # 结果:1.1

# 默认6位小数,用科学计数法

print('%e' % 1.11) # 结果:1.110000e+00

# 取3位小数,用科学计数法

print('%.3e' % 1.11) # 结果:1.110e+00

# 默认6位有效数字

print('%g' % 1111.1111) # 结果:1111.11

# 取7位有效数字

print('%.7g' % 1111.1111) # 结果:1111.111

# 取2位有效数字,自动转换为科学计数法

print('%.2g' % 1111.1111) # 结果:1.1e+03

format的使用

位置匹配

① 不带参数,即{}

② 带数字参数,可调换顺序,即{1}、{2}

③ 带关键字,即{a}、{to}

# 不带参数

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('{a} {tom} {a}'.format(tom='hello',a='world')) # 结果:world hello world

# 通过索引

coord = (3, 5)

print('X: {0[0]}; Y: {0[1]}'.format(coord)) # 结果:'X: 3; Y: 5'

# 通过key键参数

a = {'a': 'test_a', 'b': 'test_b'}

print('X: {0[a]}; Y: {0[b]}'.format(a)) # 结果:'X: test_a; Y: test_b'

格式转换

符号

描述

'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%

高阶用法

进制转换

print("int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42))

# 输出:int: 42; hex: 2a; oct: 52; bin: 101010

print("int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42))

# 在前面加“#”,则带进制前缀

# 输出:int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010

左中右对齐及位数补全

㈠ 对齐

符号

描述

<

左对齐(默认)

>

右对齐

^

居中对齐

=

在小数点后进行补齐(只用于数字)

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

# 默认左对齐

print('{} and {}'.format('hello','world')) # 结果:hello and world

# 取10位左对齐,取10位右对齐

print('{:10s} and {:>10s}'.format('hello','world')) # 结果:hello and world

# 取10位中间对齐

print('{:^10s} and {:^10s}'.format('hello','world')) # 结果: hello and world

# 取2位小数

print('{} is {:.2f}'.format(1.123,1.123)) # 结果:1.123 is 1.12

# 取2位小数,右对齐,取10位

print('{0} is {0:>10.2f}'.format(1.123)) # 结果:1.123 is 1.12

# 左对齐

print('{:<30}'.format('left aligned')) # 结果:'left aligned '

# 右对齐

print('{:>30}'.format('right aligned')) # 结果:' right aligned'

# 中间对齐

print('{:^30}'.format('centered')) # 结果:' centered '

# 使用“*”填充

print('{:*^30}'.format('centered')) # 结果:'***********centered***********'

# 还有“=”只能应用于数字,这种方法可用“>”代替

print('{:0=30}'.format(11)) # '000000000000000000000000000011'

正负符号显示

正负符号显示 %+f, %-f, 和 % f的用法

# 总是显示符号

print('{:+f}; {:+f}'.format(3.14, -3.14)) # '+3.140000; -3.140000'

# 若是+数,则在前面留空格

print('{: f}; {: f}'.format(3.14, -3.14)) # ' 3.140000; -3.140000'

# -数时显示-,与'{:f}; {:f}'一致

print('{:-f}; {:-f}'.format(3.14, -3.14)) # '3.140000; -3.140000'

百分数%

points = 19

total = 22

print('Correct answers: {:.2%}'.format(points/total)) # 'Correct answers: 86.36%'

逗号作为千位分隔符,金额表示

print('{:,}'.format(1234567890)) # '1,234,567,890'

format变形用法

在字符串前加f以达到格式化的目的,在{}里加入对象,此为format的另一种形式

name = 'jack'

age = 18

sex = 'man'

job = "IT"

salary = 9999.99

print(f'my name is {name.capitalize()}.') # my name is Jack.

print(f'I am {age:*^10} years old.') # I am ****18**** years old.

print(f'I am a {sex}') # I am a man

print(f'My salary is {salary:10.3f}') # My salary is 9999.990

python数据的格式输出_Python格式化输出相关推荐

  1. python输出的格式控制符_Python格式化输出

    说到格式化输出就要先说说print函数了 python的格式化输出就是对python的字符串进行一系列的操作,从而使字符串在屏幕中显示一定的格式.常见的格式化有字符串拼接.对齐. % 形式 使用 % ...

  2. python语言格式化输出_Python | 格式化输出字符串

    一直以来,字符串的格式化输出对于编程来说,尤其是新手,还是挺麻烦的.对于这部分的内容,笔者的建议是,只要大致能满足输出要求,越简单越好,别整那复杂的~(图文无关[俏皮]) 工具/原料 Python 2 ...

  3. python a和b字符串和占位符输出_python格式化输出

    python格式化输出 一.格式化输出三种方式 在写程序的过程中难免要得,输出有有特色的格式来,没有特色,谁还会耐得下去看呢,恐怕自己都写小烦躁,不要不要得,毕竟人就是个神奇的小动物嘛,所以说不得不提 ...

  4. python保留两位小数_python格式化输出保留2位小数的实现方法

    我是小白就不用多说了,学习python做了个练习题,结果运行了一遍,发现输入金额后得到的有很多位小数, 虽然不知道为什么,但是看得很不舒服, 就想到应该把让小数点后只保留2位数 找到了方法:将{0}改 ...

  5. 以列表形式输出_python格式化输出总结

    % a = 3.14151617 print('The number is %f'%a) #浮点数输出,小数点后保留6位有效数字 print('The number is %.3f'%a) #浮点数, ...

  6. python输入输出-2. Python中的基本输入、输出、格式化输出

    本文利用的是Python 3.x版本,建议学习3.x版本 Python中的基本输入.输出.格式化输出 1. 输入 使用input([prompt])读取一行,将其转换为string类型并返回,inpu ...

  7. python数据的格式输出_python数据类型,格式话输出

    一.程序交互 name = input("你的名字是:") #用户输入,输入的任何东西都存储成str(字符串类型)的形式 二.注释的重要性 以后动辄几千行代码的时候,回过头再去看的 ...

  8. python数据的格式输出_python

    本文主要由Python String Format 一文翻译整理而来,在python中使用 % 进行格式化字符串由来已久,不过在python2.7+ 的版本中引入了新的格式化字符串的方法.虽然%号的方 ...

  9. python的format用法打印表格_Python格式化输出——format用法示例

    format OR % 提到Python中的格式化输出方法,一般来说有以下两种方式: print('hello %s' % 'world') # hello world print('hello {} ...

最新文章

  1. 【运维学习笔记】运维入门
  2. 找到那些新时代的“操作系统”
  3. QIIME 2教程. 22命令行界面q2cli(2020.11)
  4. COM原理及应用之COM特性
  5. 实验新手必须知道的细胞计数技巧
  6. matlab 小波启发式阈值滤波,小波阈值去噪
  7. 数据库大作业-学生宿舍管理系统
  8. keep practicing for fast tying
  9. Unity GPS定位之逆地理编码(获取经纬度并转换成地理位置)
  10. 数字控制增益的放大器的设计
  11. hive正则表达式匹配中文或者字符
  12. 医院预约挂号小程序 毕业设计毕业论文 开题报告和效果图参考(基于微信小程序毕业设计题目选题课题)
  13. 网站丨怎有怎么多神奇的网站呀
  14. 基于PHP音乐网站平台系统设计与实现 开题报告
  15. 马来西亚银行集团CIMB加入Ripple区块链支付网络
  16. JVM编译过程与后期的优化
  17. 数据库的目录IDF打不开!附加失败
  18. Open Inventor: Windows系统下编译安装Quater
  19. delphi修改电脑IP
  20. win10专业版 安装 docker

热门文章

  1. linux firefox 脚本,linux下调整firefox的有用设置(高分辨率下需要)
  2. 再有人问你MySql的隔离级别,直接把这篇文章发给他!
  3. 你以为用了BigDecimal后,计算结果就一定精确了?
  4. Android软件开发之盘点所有Dialog对话框大合集(一)
  5. QT5快速转换路径(/斜杠与\反斜杠转换)
  6. 全网最详细的docker配置nginx http2 优化高速访问
  7. 关于map对key自定义排序
  8. ASP.NET MVC中使用Autofac实现简单依赖注入
  9. 使用函数统计指定c语言,浙大版《C语言程序设计(第3版)》题目集 习题5-5 使用函数统计指定数字的个数...
  10. hive启动报错:Exception in thread “main“ java.lang.NoSuchMethodError: com.google.common.base.Precondition