使用方法:  '{}bbccc'.format(aa) = aabbcc, 用来代替python2中的%,即替换。

1、通过位置来指定替换

In [2]: '{0},{1}'.format('a', 'b')
Out[2]: 'a,b' In [3]: '{1},{0}'.format('a', 'b')
Out[3]: 'b,a' In [4]: '{},{}'.format('a', 'b')
Out[4]: 'a,b'

2、通过关键值参数来指定

In [5]: '{a},{b}'.format(a='d', b='e')
Out[5]: 'd,e' In [6]: '{b},{a}'.format(a='d', b='e')
Out[6]: 'e,d'

3、通过列表或字典

In [7]: a = ['c', 'd']

In [8]: '{0[0]}, {0[1]}'.format(a)
Out[8]: 'c, d' In [9]: '{n[0]}, {n[1]}'.format(a) 这里应该使用关键字参数
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-9-e2eb21499a9d> in <module>()
----> 1 '{n[0]}, {n[1]}'.format(a)
KeyError: 'n' In [10]: '{n[0]}, {n[1]}'.format(n=a)
Out[10]: 'c, d' In [11]: b = {'x': 1, 'y': 2} In [12]: '{0[x]}, {0[y]}'.format(b)
Out[12]: '1, 2'

4、填从,对奇功能,

^, <, > 分别为居中,左对齐,右对齐,
In [13]: '{:^14}'.format('nnnn')   14指定这个str共多少个字符,
Out[13]: ' nnnn ' ^ 为居中 In [14]: '{:>14}'.format('nnnn') > 为右对齐
Out[14]: ' nnnn' In [15]: '{:<14}'.format('nnnn') < 为左对齐
Out[15]: 'nnnn ' In [16]: '{:0<14}'.format('nnnn') 冒号后面根填从,
Out[16]: 'nnnn0000000000' In [17]: '{:b<14}'.format('nnnn')
Out[17]: 'nnnnbbbbbbbbbb'

5、转换格式,

一个对象本身不是str,ascii,repr格式,可以使用!s、!a、!r,将其转成str,ascii,repr。

>>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')
"repr() shows quotes: 'test1'; str() doesn't: test2"

还有很多功能,请查看官网:https://docs.python.org/3/library/string.html#formatspec

比如:

>>> import datetime
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58' >>> points = 19
>>> total = 22
>>> 'Correct answers: {:.2%}'.format(points/total)
'Correct answers: 86.36%' >>> '{:,}'.format(1234567890)
'1,234,567,890' >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always
'+3.140000; -3.140000'
>>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers
' 3.140000; -3.140000'
>>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}'
'3.140000; -3.140000'

python的format格式化的更多相关文章

  1. 转载:python的format格式化输出

    https://www.cnblogs.com/chunlaipiupiupiu/p/7978669.html python中format函数   ---恢复内容开始--- python中format ...

  2. Python用format格式化字符串

    format是是python2.6新增的一个格式化字符串的方法,相对于老版的%格式方法,它有很多优点. 1.不需要理会数据类型的问题,在%方法中%s只能替代字符串类型 2.单个参数可以多次输出,参数顺 ...

  3. Python print format() 格式化内置函数

    Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 : 来代替以前的 % . format 函数可以接受不限个参数 ...

  4. python中format格式化函数

    http://www.runoob.com/python/att-string-format.html

  5. Python中用format函数格式化字符串

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存. 1.百分号方式 语法:%[( ...

  6. Python中用format函数格式化字符串的用法

    这篇文章主要介绍了Python中用format函数格式化字符串的用法,格式化字符串是Python学习当中的基础知识,本文主要针对Python2.7.x版本,需要的朋友可以参考下   自python2. ...

  7. Python 3.x 格式化输出字符串 % & format 笔记

    Python 3.x 格式化输出字符串 % & format 笔记 python格式化字符串有%和{}两种 字符串格式控制符. 字符串输入数据格式类型(%格式操作符号) %%百分号标记 %c字 ...

  8. #python str.format 方法被用于字符串的格式化输出。

    #python str.format 方法被用于字符串的格式化输出. #''.format() print('{0}+{1}={2}'.format(1,2,3)) #1+2=3 可见字符串中大括号内 ...

  9. 【387】Python format 格式化函数

    参考:Python format 格式化函数 # 保留小数点后两位 f'{3.1415926:.2f}' # 带符号保留小数点后两位 f'{3.1415926:+.2f}' f'{-1:+.2f}' ...

随机推荐

  1. Jmeter对jar包的调用赋值

    一.前言 在我们测试接口的过程中,可能有时需要用到第三方jar包来生成一些测试数据(如有时需要对参数的输入值使用第三方jar包进行加密操作),涉及到这种的情况,普遍做法是:手动调用jar包获得需要的值 ...

  2. Django框架---- 信号

    Django中的信号及其用法 Django中提供了"信号调度",用于在框架执行操作时解耦. 一些动作发生的时候,系统会根据信号定义的函数执行相应的操作 Django中内置的sign ...

  3. URI/URL/URN的联系和区别

    下面是我整理的一些关于他们的描述. URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源. 而URL是uniform resource locato ...

  4. openwrt修改hosts

    不同于标准linux主机,openwrt使用dnsmasq来管理dns和dhcp. 修改dnsmasq的配置文件 vi /etc/config/dhcp 在config dnsmasq这组下面添加 l ...

  5. topcoder srm 590 div1 (max_flow_template)

    problem1 link 对于每一个,找到其在目标串中的位置,判断能不能移动即可. problem2 link 如果最后的$limit$为$11=(1011)_{2}$,那么可以分别计算值为$(10 ...

  6. sql server 查询某个表被哪些存储过程调用

    sql server 查询某个表被哪些存储过程调用 select distinct object_name(id) from syscomments where id in (select id fr ...

  7. 在 jupyter 中添加菜单和自动完成功能

    在 jupyter 中添加菜单和自动完成功能 参考文档http://www.360doc.com/content/17/1103/14/1489589_700569828.shtmlhttp://to ...

  8. 剑指offer 01:二维数组中的查找

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...

  9. NYOJ 542 试制品(第五届河南省省赛)

    解法不唯一,但是还是set好理解而且用着爽,代码注释应该够详细了 #include<stdio.h> #include<string.h> #include<math.h ...

  10. Android 回退键监听

    回退键(back)监听:方法1:回调方法onBackPressed String LOG_TAG="TAG";  @Override    public void onBackPr ...