Python 字符串格式化输出方式
字符串格式化有两种方式:百分号方式、format方式。
其中,百分号方式比较老,而format方式是比较先进的,企图替代古老的方式,目前两者共存。
1、百分号方式
格式:%[(name)][flags][width].[precision]typecode
- (name) 可选,用于选择指定的key
- flags 可选,可供选择的值有:
- + 右对齐:正数的加正号,负数的加负号
- - 左对齐:正数前没有负号,负数前加负号
- width 可选,占有宽度
- .precision 可选,小数点后保留的位数
- typecode 必选
- s,获取传入的对象__str__方法的返回值,并将其格式化到指定位置
- r,获取传入对象的__repr__方法的返回值,并将其格式化到指定位置
- c,整数:将数字转换成其unicode对应的值,10进制范围为0 <= i <=1114111
- o,将整数转换成八进制表示,并将其格式化到指定位置
- x,将整数转换成16进制,并将其格式化到指定位置
- d,将整数,浮点数转化为十进制表示,并将其格式化到指定位置
例子:
>>> s = 'hello, %s!' % 'python'
>>> s
'hello, python!' >>> s = 'hello, %s, %d!' % ('python', 2018)
>>> s
'hello, python, 2018!' >>> s = 'hello, %(name)s, %(year)d!' % {'name': 'python', 'year': 2018}
>>> s
'hello, python, 2018!' >>> s = 'hello, %(name)+10s, %(year)-10d!' % {'name': 'python', 'year': 2018}
>>> s
'hello, python, 2018 !' >>> s = 'hello, %(name)s, %(year).3f!' % {'name': 'python', 'year': 2018}
>>> s
'hello, python, 2018.000!'
%r 与 %s 区别:
%r 用来做 debug 比较好,因为它会显示变量的原始数据(raw data),而其它的符号则是用来向用户显示输出的。
>>> a = 'sunday'
>>> print("Today is %s" % a)
Today is sunday
>>> print("Today is %r" % a)
Today is 'sunday' # 格式化部分用单引号输出 >>> from datetime import datetime
>>> d = datetime.now()
>>> print('%s' % d)
2018-09-10 08:52:00.769949
>>> print('%r' % d)
datetime.datetime(2018, 9, 10, 8, 52, 0, 769949) # 可以看见与上面输出存在明显的区别
2、format方式
>>> s = 'hello, {}, {}'.format('python', 2018)
>>> s
'hello, python, 2018' >>> s = 'hello, {0}, {1}, hi, {0}'.format('python', 2018)
>>> s
'hello, python, 2018, hi, python' >>> s = 'hello, {name}, {year}, hi, {name}'.format(name='python', year=2018)
>>> s
'hello, python, 2018, hi, python' >>> s = 'hello, {:s}, {:d}, hi, {:f}'.format('python', 2018, 9.7)
>>> s
'hello, python, 2018, hi, 9.700000'
Python 字符串格式化输出方式的更多相关文章
- python字符串格式化输出
python格式化输出 python格式化输出有两种方式:百分号和format format的功能要比百分号方式强大,其中format独有的可以自定义字符填充空白.字符串居中显示.转换二进制.整数自动 ...
- python 字符串格式化输出 %d,%s及 format函数
旧式格式化方式:%s,%d 1.顺序填入格式化内容 s = "hello %s, hello %d"%("world", 100) print(s) 结果: ' ...
- python字符串格式化输出 %和format举例
#!/usr/bin/env python # -*- coding: UTF-8 -*- #pyversion:python3.5 #owner:fuzj s1 = "i am %s, i ...
- python 字符串格式化 输出
1. 需要输出3列,为了输出好看,需要制定每一列的宽度: ‘%6.2f’ % 1.235 # 长度为6,保留2为小数 print '{0:20} {1:<20} {1:<20}\r\n'. ...
- python之格式化输出(3种方式)
python3.6后支持3种格式化输出方式,其中前两种为%-formatting及str.format ,第三种即为 f-string. 1.%-formatting 据传该格式化方法源于C.. &g ...
- (Python )格式化输出、文件操作、json
本节学习Python的格式化输出,文件操作以及json的简单用法 1.格式化输出 将非字符串类型转换成字符串,可以使用函数:str() 或者repr() ,(这两个函数的区别目前我还没搞懂,求解答) ...
- Python基本格式化输出
什么叫格式化输出? 数据按照某种特殊的要求输出 假如输入一个整数,希望整数按照十六进制,八进制输出,如果输入一个小数,希望小数保留后面2位数然后输出,或者以科学计数法的方式来输出小数.字符串的输出希望 ...
- python字符串格式化之学习笔记
在python中格式化输出字符串使用的是%运算符,通用的形式为 •格式标记字符串 % 要输出的值组其中,左边部分的”格式标记字符串“可以完全和c中的一致.右边的'值组'如果有两个及以上的值则需要用小括 ...
- Python学习:12.Python字符串格式化
字符串格式化 讲解Python这么久,也没有讲解Python的字符串的格式化,那我们今天就来了解一下python字符串格式化的强大之处. 首先我们先理解一下为什么要有字符串的格式化,就是为了方便字符串 ...
随机推荐
- Altium_Designer-怎么将“原理图的更改”更新到“pcb图”?
打开原理图,直击菜单栏>>Design,选择第一项,>>Update PCB Document...在弹出的对话框里面选择执行更改即可将原理图更新到工程下面对应的PCB.也可以 ...
- 814. Binary Tree Pruning(leetcode) (tree traverse)
https://leetcode.com/contest/weekly-contest-79/problems/binary-tree-pruning/ -- 814 from leetcode tr ...
- Django QuestSet API (官方文档)
1.返回新查询集的方法 (1)filter():滤指定条件的结果 Entry.objects.filter(pub_date__gt=datetime.date(2005, 1, 3), headli ...
- IOS NSOperationQueue(线程 封装操作)
#import "HMViewController.h" @interface HMViewController () @end @implementation HMViewCon ...
- 字符串查找算法的改进-hash查找算法
字符串查找即为特征查找: 特征即位hash: 1.将待查找的字符串hash: 2.在容器字符串中找头字符匹配的字符串,并进行hash: 3.比较hash的结果:相同即位匹配: hash算法的设计为其中 ...
- android获取传感器数据
传感器获取数据的频率: https://blog.csdn.net/huangbiao86/article/details/6745933 SensorManager.SENSOR_DELAY_GAM ...
- PASCAL VOC数据集分析
http://blog.csdn.net/zhangjunbob/article/details/52769381
- a=a+(a++);b=b+(++b);计算顺序,反汇编
a=a+(a++); 013913BC mov eax,dword ptr [a] 013913BF add eax,dword ptr [a] 013913C2 mov dword ptr [a], ...
- 其它内置函数(zip等)
python内置函数 截止到python版本3.6.2,python一共为我们提供了68个内置函数.它们就是python提供给你直接可以拿来使用的所有函数 Built-in Functio ...
- Linux 安装Oracle11g完整安装图文教程另附基本操作 (分享)
一.修改操作系统核心参数 在Root用户下执行以下步骤: 1)修改用户的SHELL的限制,修改/etc/security/limits.conf文件 输入命令:vi /etc/security/lim ...