python format函数的使用
转载自:http://www.cnblogs.com/kaituorensheng/p/5709970.html
python自2.6后,新增了一种格式化字符串函数str.format(),威力十足,可以替换掉原来的%
注:以下操作版本是python2.7
映射示例
语法
通过{} 和 : 替换 %
通过位置
>>> '{0} is {1}'.format('jihite', '4 years old')
'jihite is 4 years old'
>>> '{0} is {1} {0}'.format('jihite', '4 years old')
'jihite is 4 years old jihite'
通过format函数可以接受不限参数个数、不限顺序
通过关键字
>>> '{name}:{age}'.format(age=4,name='jihite')
'jihite:4'
>>> '{name}:{age}'.format(age=4,name='jihite',locate='Beijing')
'jihite:4'
format括号内用=给变量赋值
通过对象属性

>>> class Person:
... def __init__(self, name, age):
... self.name,self.age = name, age
... def __func__(self):
... return "This guy is {self.name}, is {self.age} old".format(self=self)
...
>>> s =Person('jihite', 4)
>>> s.__func__()
'This guy is jihite, is 4 old'

通过下标
>>> '{0[0]} is {0[1]} years old!'.format(['jihite', 4])
'jihite is 4 years old!'
>>> '{0} is {1} years old!'.format('jihite', 4)
'jihite is 4 years old!'
其实就是通过位置
格式限定符
通过{} : 符号
填充和对齐
^<>分别表示居中、左对齐、右对齐,后面带宽度

>>> '{:>10}'.format('jihite')
' jihite'
>>> '{:<10}'.format('jihite')
'jihite '
>>> '{:^10}'.format('jihite')
' jihite '

精度和类型f
精度常和f一起使用
>>> '{:.2f}'.format(3.1415)
'3.14'
>>> '{:.4f}'.format(3.1)
'3.1000'
进制转化

>>> '{:b}'.format(10)
'1010'
>>> '{:o}'.format(10)
'12'
>>> '{:d}'.format(10)
'10'
>>> '{:x}'.format(10)
'a'

其中b o d x分别表示二、八、十、十六进制
千位分隔符

>>> '{:,}'.format(1000000)
'1,000,000'
>>> '{:,}'.format(100000.23433)
'100,000.23433'
>>> '{:,}'.format('abcedef')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Cannot specify ',' with 's'.
尤其是其中的精度与类型,用起来很方便
python format函数的使用的更多相关文章
- python format()函数的用法
Python format() 函数的用法 复制自博主 chunlaipiupiupiu 的博客,如有侵权,请联系删除 python中format函数用于字符串的格式化 通过关键字 1 print(' ...
- python format函数/print 函数详细讲解(4)
在python开发过程中,print函数和format函数使用场景特别多,下面分别详细讲解两个函数的用法. 一.print函数 print翻译为中文指打印,在python中能直接输出到控制台,我们可以 ...
- python format() 函数
转载 https://www.cnblogs.com/wushuaishuai/p/7687728.html 正文 Python2.6 开始,新增了一种格式化字符串的函数 format() ,它增强了 ...
- python - format函数 /class内置format方法
format函数 # format函数 # 用于字符串格式化 # 基本用法: # 方式一:(位置方式) x = "{0}{1}{2}".format(1,2,3) print('1 ...
- 第三章:Python基础の函数和文件操作实战
本課主題 Set 集合和操作实战 函数介紹和操作实战 参数的深入介绍和操作实战 format 函数操作实战 lambda 表达式介绍 文件操作函数介紹和操作实战 本周作业 Set 集合和操作实战 Se ...
- python range函数(42)
在python中使用最多的除了print函数 就是 for循环 了,那么这里就不得不介绍一下python内置函数range函数! 一.range函数简介 python range函数可创建一个整数列表 ...
- 飘逸的python - 增强的格式化字符串format函数
自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...
- Python中用format函数格式化字符串的用法
这篇文章主要介绍了Python中用format函数格式化字符串的用法,格式化字符串是Python学习当中的基础知识,本文主要针对Python2.7.x版本,需要的朋友可以参考下 自python2. ...
- 【python】format函数格式化字符串的用法
来源:http://www.jb51.net/article/63672.htm 自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式 ...
随机推荐
- mysql启动以及常用命令汇总
mysql57的启动 常用命令 : show databases : 展示所有数据库 use 数据库名 : 连接数据库 show tables ...
- wndr4300刷任意系统及刷回官方原厂系统
4300是目前性价比比较高的可玩路由器了,如果要买的话要买v1版本的,目前卖的都是v2,v2刷不了第三方系统. 注意:如果带宽低于50M,可以随便刷第三方系统玩,如果高于50M的带宽或者想组建千兆局域 ...
- linux 给指定用户分配文件夹权限
1.更改目录所有者命令:chown -R 用户名称 目录名称2.更改目录权限命令:chmod -R 755 目录名称3.查看文件夹的权限ls -la 目录
- java并发编程笔记(七)——线程池
java并发编程笔记(七)--线程池 new Thread弊端 每次new Thread新建对象,性能差 线程缺乏统一管理,可能无限制的新建线程,相互竞争,有可能占用过多系统资源导致死机或者OOM 缺 ...
- 热经-北京中地时空数码科技有限公司-研发工程师(WEBGIS方向)
一面: 登记,填写个人信息 笔试 选择题: HTML,CSS,JS 的选择题,都是基础题.其中有一道问哪个不是 document 的属性或方法,我在 bgColor 和 focus() 上面纠结了一下 ...
- JarvisOJ 逆向Writeup
1. 爬楼梯 先运行apk,查看具体的功能 爬一层楼是可以点击的,爬到了,看FLAG是不可以点击的.我们可以大致的了解到到了具体的楼层才可以看到flag,多次打开软件,楼层数目是随机的. 用APKID ...
- iview+vue 表格中添加图片
开门见山,话不多说,要在表格中添加图片,可以使用td: <table " width="100%"> <tr class="tr-style ...
- 牛客 在其他数都出现k次的数组中找到出现1次的数
题目链接:https://www.nowcoder.com/practice/26e46f1f5e0d48c4b9ba13fe3e8d0ec6?tpId=101&tqId=33216& ...
- CSS实现文字阴影的效果
CSS中有两种阴影效果,一种是DropShadow(投影),另一种是Shadow(阴影).1.DropShadow语法:{FILTER:DropShadow(Color=color,OffX=offX ...
- 用webdriver模仿浏览器 爬取豆瓣python书单
用webdriver模仿浏览器 爬取豆瓣python书单 其中运用到os 模块 作用是生成文件夹 存储爬取的信息 etree 用于xpath解析内容 详细代码如下 可用我的上一篇博客存取到excel当 ...