平时只用参数匹配,偶尔看到别人的format用法楞住没反应过来,遂记下

#通过位置
print '{0},{1}'.format('hehe',20) print '{},{}'.format('hehe',20) print '{1},{0},{1}'.format('hehe',20) #通过关键字参数
print '{name},{age}'.format(age=18,name='hehe') class Person:
def __init__(self,name,age):
self.name = name
self.age = age def __str__(self):
return 'This guy is {self.name},is {self.age} old'.format(self=self) print str(Person('hehe',18)) #通过映射 list
a_list = ['hehe,20,'china']
print 'my name is {0[0]},from {0[2]},age is {0[1]}'.format(a_list)
#my name is hehe,from china,age is 20 #通过映射 dict
b_dict = {'name':'hehe','age':20,'province':'shanxi'}
print 'my name is {name}, age is {age},from {province}'.format(**b_dict)
#my name is hehe, age is 20,from shanxi #填充与对齐
print '{:>8}'.format('189')
# 189
print '{:0>8}'.format('189')
#00000189
print '{:a>8}'.format('189')
#aaaaa189 #精度与类型f
#保留两位小数
print '{:.2f}'.format(321.33345)
#321.33 #用来做金额的千位分隔符
print '{:,}'.format(1234567890)
#1,234,567,890 #其他类型 主要就是进制了,b、d、o、x分别是二进制、十进制、八进制、十六进制。 print '{:b}'.format(18) #二进制 10010
print '{:d}'.format(18) #十进制 18
print '{:o}'.format(18) #八进制 22
print '{:x}'.format(18) #十六进制12

python中format所有用法的更多相关文章

  1. Python中format的用法

    自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱.语法 它通过{}和: ...

  2. python 中del 的用法

    python中的del用法比较特殊,新手学习往往产生误解,弄清del的用法,可以帮助深入理解python的内存方面的问题. python的del不同于C的free和C++的delete. 由于pyth ...

  3. python中argparse模块用法实例详解

    python中argparse模块用法实例详解 这篇文章主要介绍了python中argparse模块用法,以实例形式较为详细的分析了argparse模块解析命令行参数的使用技巧,需要的朋友可以参考下 ...

  4. python中format函数

    python中format函数用于字符串的格式化 通过关键字 1 print('{名字}今天{动作}'.format(名字='陈某某',动作='拍视频'))#通过关键字 2 grade = {'nam ...

  5. 【313】python 中 print 函数用法总结

    参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...

  6. python中MySQLdb模块用法实例

    篇文章主要介绍了python中MySQLdb模块用法,以实例形式详细讲述了MySQLdb模块针对MySQL数据库的各种常见操作方法,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了python中 ...

  7. python中format函数用于字符串的格式化

    python中format函数用于字符串的格式化 通过关键字 print('{名字}今天{动作}'.format(名字='陈某某',动作='拍视频'))#通过关键字 grade = {'name' : ...

  8. python中hashlib模块用法示例

    python中hashlib模块用法示例 我们以前介绍过一篇Python加密的文章:Python 加密的实例详解.今天我们看看python中hashlib模块用法示例,具体如下. hashlib ha ...

  9. Python Deque 模块使用详解,python中yield的用法详解

    Deque模块是Python标准库collections中的一项. 它提供了两端都可以操作的序列, 这意味着, 你可以在序列前后都执行添加或删除. https://blog.csdn.net/qq_3 ...

随机推荐

  1. springboot备忘

    1.springboot中有ApplicationRunner类,如果项目中的启动类名称也是ApplicationRunner,单元测试时需要注意:import不要import到springboot的 ...

  2. SprintBoot日志

    yml配置 #logging logging.file: "logs/app.log" logging: level: root: debug file: max-size: 10 ...

  3. 本地oracle可以通过localhost连接,无法通过ip地址连接解决方法,oracle远程连接配置

    Oracle11g安装后只有本地可以连接,远程无法连接,而且本地只能配置成localhost配置成IP地址也无法连接. 这是因为安装oracle的时候没有配置远程的监听,默认的监听是localhost ...

  4. [转] An In-Depth Look at the HBase Architecture - HBase架构深度剖析

    [From] https://mapr.com/blog/in-depth-look-hbase-architecture/ In this blog post, I’ll give you an i ...

  5. String与toString

    String与toString能将对象转为字符串: 类型 行为描述 Array 将 Array 的每个元素转换为字符串,并将它们依次连接起来,两个元素之间用英文逗号作为分隔符进行拼接. Boolean ...

  6. python学习之那些你不在乎却操作非主流的练习题(一)

    1.写代码,有如下列表,按照要求实现每⼀个功能 lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt ...

  7. 安装 Git 并连接 Github

    下载安装 Git, 下载地址:https://git-scm.com/download/win . 在命令行中输入 git 测试 Git 是否安装成功. 在桌面鼠标右击打开 Git Bash Here ...

  8. 一加手机刷入第三方Rec

    首先阐述一下刷机的整体流程: 备份数据(可选):短信.联系人.通话记录.图片.应用数据的云端同步. 解锁 刷入第三方Recovery(简称Rec). 进入第三方Rec,刷第三方ROM. 刷机成功 解锁 ...

  9. Angular中引入外部js的使用方式

    在Angular中我们或许会用到部分外部插件的时候,像Bootstrap,Jquery这些当然我们可以通过Npm安装包的形式引入,但是还有一些其它的js库需要引入的话,我们又应该怎样操作呢? 在这里做 ...

  10. jenkins和gitlab备份

    #把/opt目录拷贝到10.0.0.31的/tmp下. rsync  -avz  /opt  root@10.0.0.31:/tmp/ pull: rsync -avz root@10.0.0.31: ...