平时只用参数匹配,偶尔看到别人的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. mysql 查看库结构---查看表结构

    查看库结构:SHOW DATABASES; 查看表结构: show tables即为显示当前数据库中所有的表.又如: mysql> use dbname //进入dbname库Database ...

  2. Log4J日志组件

    Log4j,  log for java, 开源的日志组件! 使用步骤: 1. 下载组件,引入jar文件; log4j-1.2.11.jar 2. 配置 :  src/log4j.properties ...

  3. 使用Statement执行DML和DQL语句

    import com.loaderman.util.JdbcUtil; import java.sql.Connection; import java.sql.DriverManager; impor ...

  4. 关于Android Studio加载.so文件问题

    在main文件下创建jniLibs文件,然后把.so文件copy过去,然后在 app的 build.gradle里面添加如下代码 sourceSets { main { jniLibs.srcDirs ...

  5. OpenStack Blazar 架构解析与功能实践

    目录 文章目录 目录 Blazar Blazar 的安装部署 Blazar 的软件架构 Blazar 的资源模型与状态机 Blazar 的主机资源预留功能(Host Reservation) 代码实现 ...

  6. Selenium下Chrome配置

    地址:https://peter.sh/experiments/chromium-command-line-switches/ chrome_options.add_argument('--headl ...

  7. 14 count(*)

    14 count(*) count(*)实现方式 首先要声明,在不同的mysql引擎中,count(*)有不同的实现方式. --myisam引擎把一个表的总行数存在了磁盘,因此执行count(*)的时 ...

  8. 【转】HBase shell命令与 scan 过滤器

    Hbase 常用shell命令 https://www.cnblogs.com/i80386/p/4105423.html HBase基础之常用过滤器hbase shell操作 https://www ...

  9. Qt QLabel添加cliked事件

    #ifndef MYLABEL_H #define MYLABEL_H #include <QObject> #include <QLabel> class MyLabel : ...

  10. staticmethod自己定制

    class StaticMethod: def __init__(self,func): self.func=func def __get__(self, instance, owner): #类来调 ...