python字符串打印教学_python字符串格式化教你正确打印 : D

python字符串格式化教你正确打印 : D

python字符串格式化教你正确打印 : D

文章目录

用格式字符%

字符串

整数

浮点数

format格式化

print需要注意的地方

转义字符

%运算符用来格式化字符串。在字符串内部,%s表示用字符串替换,%d表示用整数替换,有几个%?占位符,后面就跟几个变量或者值,顺序要一一对应。如果只有一个%?,括号可以省略。用%%来表示一个%

举栗子

>>> print('%s'%'hello world')

hello world

>>> print('%s%s'%('hello','hi'))

hellohi

>>> print('%s,%s'%('hello','hi'))

hello,hi

分数据类型说明

字符串

%s直接输出字符串

%20s右对齐,取20位,不够则补位

%-20s左对齐,取20位,不够则补位

%.2s截取2位字符串

%20.2s20位占位符,截取2位字符串

>>> print('%s' % 'hello world')

hello world

>>> print('%20s' % 'hello world')

hello world

>>> print('%-20s' % 'hello world')

hello world

>>> print('%020s' % 'hello world')

hello world

>>> print('%.2s' % 'hello world')

he

>>> print('%20.2s' % 'hello world')

he

>>> print('%-20.2s' % 'hello world')

he

>>>

整数

%o八进制

%d十进制

%x十六进制

%10d右对齐,取10位,不够则补位

%-10d左对齐,取10位,不够则补位

%010d右对齐,取10位,不够则补0

%-010d左对齐,取10位,不够则补0(0不显示)

%+010d右对齐,显示正号,取10位,不够则补0

%-+010d左对齐,显示正号,取10位,不够则补0(0不显示)

>>> print('%d'% 1234)

1234

>>> print('%o'% 1234)

2322

>>> print('%x'% 1234)

4d2

>>> print('%10d'% 1234)

1234

>>> print('%-10d'% 1234)

1234

>>> print('%010d'% 1234)

0000001234

>>> print('%-010d'% 1234)

1234

>>> print('%-+010d'% 1234)

+1234

>>> print('%+010d'% 1234)

+000001234

>>> print('%-+010.5d'% 1234)

+01234

>>> print('%-+010.6d'% 1234)

+001234

>>> print('%+010.6d'% 1234)

+000001234

浮点数

%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

format方法所做的事情就是将参数值替换至格式所在的位置

print('{} was {} years old when he wrote this book'.format(name, age))

不带编号,{}

带数字编号,可调换顺序{1},{2}

带关键字,{a},{b}

>>> print('{} was {} years old when he wrote this book'.format('name', 'age'))

name was age years old when he wrote this book

>>> print('{0} was {1} years old when he wrote this book'.format('name', 'age'))

name was age years old when he wrote this book

>>> print('{1} was {0} years old when he wrote this book'.format('name', 'age'))

age was name years old when he wrote this book

这之中还可以有更详细的格式

# 对于浮点数 '0.333' 保留小数点(.)后三位

print('{0:.3f}'.format(1.0/3))

# 使用下划线填充文本,并保持文字处于中间位置

# 使用 (^) 定义 '___hello___'字符串长度为 11

print('{0:_^11}'.format('hello'))

# 基于关键词输出 'Swaroop wrote A Byte of Python'

print('{name} wrote {book}'.format(name='Swaroop', book='A Byte of Python'))

模板字符串的槽除了包括参数序号,还可以包括格式控制信息 {: }

格式控制标记包括:,<.>6个字段。

指当前槽的设定输出宽度。

如果实际format()参数实际宽度比设定大,使用参数实际长度

如果参数实际宽度比设定小,则多余的位数默认以空格填充,也可以设置填充符号

>>> print("{0:*>30}".format('hello')) # 30个字符,右对齐,左边补空格

*************************hello

>>> print("{0:*^30}".format('hello')) # 30个字符,居中对齐,两边补*

************hello*************

>>> print("{0:*<30}".format('hello')) # 30个字符,左对齐,右边补*

hello*************************

>>> print("{0:3}".format('hello')) # 长度小于实际字符数,按实际输出

hello

>>>

<.>表示两个含义

对于浮点数,精度表示小数部分输出部分的有效位数

对于字符串,表示输出的最大长度

>>> print("{0:.2f}".format(12345.67890)) # 保留2位小数

12345.68

>>> print("{0:*^20.2f}".format(12345.67890)) # 20个字符长度,居中对齐,两边补*,保留两位小数

******12345.68******

>>> print("{0:.4}".format("hello")) # 输出4个字符

hell

浮点数类型

e小写字母e的指数形式

E大写字母E的指数形式

f标准形式

%浮点数的百分数形式

>>> print("{0:e},{0:E},{0:f},{0:%}".format(123.456))

1.234560e+02,1.234560E+02,123.456000,12345.600000%

>>> print("{0:.2e},{0:.2E},{0:.2f},{0:.2%}".format(123.456))

1.23e+02,1.23E+02,123.46,12345.60%

print总是会以一个不可见的“新一行”字符(\n)结尾,因此重复调用 print将会在相互独立的一行中分别打印。为防止打印过程中出现这一换行符,你可以通过end指定其应以空白结尾:

print('a', end='')

print('b', end='')

输出

ab

反斜杠字符\是一个特殊字符,在字符串中表示转义,即该字符与后面相邻的一个字符组成了新的含义。

\n表示换行,\\表示反斜杠,\’表示单引号

\”表示双引号,\t表示制表符Tab

>>> print("hello\nworld\t世界你好")

hello

world世界你好

python字符串格式化教你正确打印 : D相关教程

python打包生成so文件

python打包生成so文件 python打包生成so文件 文章目录 python打包生成so文件 1.安装工具 2. Linux下的.so文件(测试用例) 3. Windows下将Python封装成pyd文件(dll) 1.安装工具 python3 安装:cython pip3 install cython -i http://mirrors.aliyun.com/pypi

python小波变换 wavedec2函数 各个返回值详解

python小波变换 wavedec2函数 各个返回值详解 网上找了好多文章都没有提到这个东西,没有说明 wavedec2 函数各个返回值究竟是什么意思 我们先看看 wavedec2 函数的大概形式, pywt.wavedec2(data, wavelet, mode=’symmetric’, level=None, axes=(-2, -1)) d

第一个博客 弄了2天的C语言数据结构字符串

第一个博客 弄了2天的C语言数据结构字符串 第一个博客 发个弄了2天的C语言数据结构字符串吧 首先 其次 先给出Str结构体的声明定义: 首次敲出来后代码大致如下: 问题发现及修正: 最后是完整的代码: 首先 这里只发KMP算法改过来的,BF算法直接去掉getnext函

零基础学习python需注意:不同语言对单例模式的不同实现

零基础学习python需注意:不同语言对单例模式的不同实现 前言 前段时间在用 Python 实现业务的时候发现一个坑,准确的来说是对于 Python 门外汉容易踩的坑; 大概代码如下: class Mom(object): name = '' sons = []if __name__ == '__main__': m1 = Mom() m1

Python爬虫数据抽取(三):pyquery库

Python爬虫数据抽取(三):pyquery库 目录 1. 基本用法 1.1 pyquery的基本用法 1.2 CSS选择器 1.3 查找子节点 1.4 查找父节点 1.5 查找兄弟节点 1.6 获取节点信息 1.7 修改节点-添加和移除节点的样式 1.8 修改节点-属性和文本内容 1.9 删除节点 1.10 伪类选择

java格式化输出问题

java格式化输出问题 Java中System.out.print();System.out.printf();System.out.println()的区别 System.out.println();输出后换行 System.out.print();输出后不换行 System.out.printf();可以实现对输出宽度的控制以及左右对齐 相关符号说明: 基本格式:格

Python爬虫练习2_小图片下载

Python爬虫练习2_小图片下载 图片下载 准备工具 前言 步骤分析与代码实现 准备工具 本机环境:Windows10专业版 操作系统:64位 Python版本:python 3.8 运行工具:Python 3.8.0 Shell 前言 今天有点难,因为我的PyCharm体验到期了,运行工具就用python3.8自带

python遇到TypeError: unhashable type: ‘list‘

python遇到TypeError: unhashable type: ‘list‘ python遇到TypeError: unhashable type: ‘list’ 今天在写这个泰坦尼克号的时候,出现了这个bug。 后来检查后,才发现Embarked这一列被我改成list类型了,自然不能够hash。因此对原始数据,重新跑一遍后,结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值