使用方法:  '{}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. Python取整及保留小数小结

    1.int() 向下取整 内置函数  n = 3.75 print(int(n))>>> 3 n = 3.25 print(int(n))>>> 3 2.round ...

  2. 网页分享到微博、QQ、QQ空间、微信

    <ul id="content-share-list" class="bdsharebuttonbox bdshare-button-style0-16" ...

  3. input 只能输入数字、字母、汉字等

    1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'')" onafte ...

  4. Fast Matrix Calculation HDU - 4965

    One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...

  5. CentOS6.5升级GCC4.8

    # curl -Lks http://www.hop5.in/yum/el6/hop5.repo > /etc/yum.repos.d/hop5.repo # cat /etc/yum.repo ...

  6. 采用xtrabackup部署主从同步

    1.停掉从库192.168.1.23[root@dev-env23 tmp]# service mysqld stopStopping mysqld: [ OK ] 2.主库全备份cd /opt/xt ...

  7. 2018-2019-1 20189206 《Linux内核原理与分析》第九周作业

    #linux内核分析学习笔记 --第八章 进程的切换和系统的一般执行过程 学习目标:重点关注进程切换的过程,进程调度的时机,操作系统的基本构成以及一般的执行过程. 进程调度的时机 因为进程的调度只发生 ...

  8. day01编程语言,计算机组成: 五大组成部分,计算机三大核心,进制,内存分布图,操作系统

    本周内容 第一天: 计算机原理 操作系统 第二天: 编程语言 python入门:环境 - 编辑器 变量 基本数据类型​ 学习方法 鸡汤 - 干货​wwwh:what | why | where | h ...

  9. 在WPF中调用文件夹浏览/选择对话框

    var dialog = new System.Windows.Forms.FolderBrowserDialog(); System.Windows.Forms.DialogResult resul ...

  10. CentOS7.3上如何安装Apache/2.4.34

    1)卸载系统自带的httpd Centos可能自带了httpd,但是版本可能会较低,执行下面的命令检测是否已经安装了httpd rpm -qa | grep httpd 如果检测已经安装了二进制的ht ...