Python:format()方法
转于:https://blog.csdn.net/zhang89xiao/article/details/53818906
博主:张肖的博客
描述:
format的格式
format_spec 的格式
format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
功能:共4个功能
一)填充字符串
#只能用{ }来放标签
A、通过位置填充字符串:{0},表示format的左起第一个元素;
print('hello {0} i am {1}, {1} love {0}'.format('MM', 'GG'))
#输出:hello MM i am GG, GG love MM
B、通过key来填充
print('hello {0} i am {g}, {g} love {0}'.format('MM', g = 'GG'))
#输出:hello MM i am GG, GG love MM
C、通过下标填充
#format的参数,当'I', names = names, u = 'U',三种混用是,'I'类型放最前;
names = ['MM', 'GG']
print('hello {names[0]}, i am {names[1]}, {0} love {u}'.format('I', names = names, u = 'U'))
#输出:hello MM i am GG, I love U
D、通过字典的key填充
names = {'m':'MM', 'g':'GG'}
print('hello {names[m]} i am {names[g]}, {i} love {u}'.format(names = names, i = 'I',u = 'U'))
#输出:hello MM i am GG, I love U
E、通过对象的属性填充
class Names():
m = 'MM'
g = 'GG' print('hello {names.m}, i am {names.g}'.format(names = Names))
#输出:hello MM, i am GG
F、使用魔法参数填充
args = ['I', 'U']
kwargs = {'m':'MM', 'g':'GG'}
print('hello {m} , i am {g}, {} love {}'.format(*args, **kwargs))
#输出:hello MM , i am GG, I love U
二)格式转换
#b、d、o、x分别是二进制、十进制、八进制、十六进制
方式一:{:条件}.format(参数)
a = 3.14159
b = '{:.2f}'.format(a)
print(b)
#输出:3.14 c = '{:+.2f}'.format(a)
print(c)
#输出:+3.14
方式二:format(参数, '.条件'),不需要{: }
# '+.2f':此处只能是具体数字2,不能是字符变量;
b = format(3.14159, '+.2f')
print(b)
#输出:+3.14
| 参数 | 条件的格式 | 输出 | 描述 |
| 3.1415926 | {:.2f} | 3.14 | 保留小数点后两位 |
| 3.1415926 | {:+.2f} | 3.14 | 带符号保留小数点后两位 |
| -1 | {:+.2f} | -1 | 带符号保留小数点后两位 |
| 2.71828 | {:.0f} | 3 | 不带小数 |
| 1000000 | {:,} | 1,000,000 | 以逗号分隔的数字格式 |
| 0.25 | {:.2%} | 25.00% | 百分比格式 |
| 1000000000 | {:.2e} | 1.00E+09 | 指数记法 |
| 25 | {0:b} | 11001 | 转换成二进制 |
| 25 | {0:d} | 25 | 转换成十进制 |
| 25 | {0:o} | 31 | 转换成八进制 |
| 25 | {0:x} | 19 | 转换成十六进制 |
三)字符串对齐于填充
方式一:{:条件}.format(参数)
方式二:format(参数, '.条件'),不需要{: }
# format(参数, '.条件')中,'0>5'中,只能是5,不能是字符变量;
| 参数 | j条件格式 | 输出 | 描述 |
| 5 | {:0>2} | 05 | 数字补零 (填充左边, 宽度为2) |
| 5 | {:x<4} | 5xxx | 数字补x (填充右边, 宽度为4) |
| 10 | {:x^4} | x10x | 数字补x (填充右边, 宽度为4) |
| 13 | {:10} | 13 | 右对齐 (默认, 宽度为10) |
| 13 | {:<10} | 13 | 左对齐 (宽度为10) |
| 13 | {:^10} | 13 | 中间对齐 (宽度为10) |
四)其它
A、转义{和}符号
|
1
|
print '{{ hello {0} }}'.format('Kevin')
|
跟%中%%转义%一样,formate中用两个大括号来转义
B、format作为函数
|
1
2
|
f = 'hello {0} i am {1}'.format
print f('Kevin','Tom')
|
C、格式化datetime
|
1
2
|
now=datetime.now()
print '{:%Y-%m-%d %X}'.format(now)
|
D、{}内嵌{}
|
1
|
print 'hello {0:>{1}} '.format('Kevin',50)
|
E、叹号的用法
# !后面可以加s r a 分别对应str() repr() ascii()
# 作用:在填充前先用对应的函数来处理参数
|
1
2
|
print "{!s}".format('2') # 2
print "{!r}".format('2') # '2'
|
差别就是repr带有引号,str()是面向用户的,目的是可读性,repr()是面向python解析器的,返回值表示在python内部的含义
ascii()一直报错,可能这个是3.0才有的函数
Python:format()方法的更多相关文章
- python的str.format方法
format方法被用于字符串的格式化输出. print('{0}+{1}={2}'.format(1,2,1+2)) #in 1+2=3 #out 可见字符串中大括号内的数字分别对应着format的几 ...
- Python里format()方法基本使用
'''第一种:自然连接''' #format 连接字符串 str = '{}使用的python是{}版本'.format('我','3.6.5') print(str) #打印结果:我使用的pytho ...
- #python str.format 方法被用于字符串的格式化输出。
#python str.format 方法被用于字符串的格式化输出. #''.format() print('{0}+{1}={2}'.format(1,2,3)) #1+2=3 可见字符串中大括号内 ...
- Python中格式化format()方法详解
Python中格式化format()方法详解 Python中格式化输出字符串使用format()函数, 字符串即类, 可以使用方法; Python是完全面向对象的语言, 任何东西都是对象; 字符串的参 ...
- Python入门之format()方法
在此列出format()方法的一些基本使用: >>> '{}{}{}'.format('圆周率是',3.1415926,'...') '圆周率是3.1415926...' >& ...
- Python 字符串格式化操作 - format方法
建议使用format()方法 字符串操作 对于 %, 官方以及给出这种格式化操作已经过时,在 Python 的未来版本中可能会消失. 在新代码中使用新的字符串格式.因此推荐大家使用format()来替 ...
- Python字符串类型格式化之format方法
python字符串格式化一般使用 format() 方法,用法如下: <模板字符串>.format(<逗号分割的参数>) 其中模板字符串中可以由一个或多个 {} 组成的 槽 , ...
- python format使用方法
#使用format 方法进行格式化 print("The number {1:} in hex is: {1:#x}, the number {0:} in oct is {0:o}&quo ...
- Python str方法总结
1.返回第一个字母大写 S.capitalize(...) S.capitalize() -> string 1 2 3 4 >>>a = 'shaw' >>> ...
- Python魔法方法总结及注意事项
1.何为魔法方法: Python中,一定要区分开函数和方法的含义: 1.函数:类外部定义的,跟类没有直接关系的:形式: def func(*argv): 2.方法:class内部定义的函数(对象的方法 ...
随机推荐
- zabbix server 端安装
1.系统环境 [root@crazy-acong ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@crazy-acong ~] ...
- java上传文件,下载文件
1.上传文件 1 protected int doTask(BaseForm form) throws AppException, FatalException, NoExistsException, ...
- Kindeditor 函数用途
1.loadScript 加载文件 2.updateState 更新工具条状态 afterCreate在dom加载的时候执行,dom加载完之前执行的 K.ready dom加载完之后执行 ...
- HDU - 2701 Lampyridae Teleportae 【模拟】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2701 题意 有一个萤火虫会闪现 一个人 也会闪现 给出 这个人的起始位置 和他能够闪现的距离 然后依次 ...
- Java编程思想(第4版) 中文清晰PDF完整版
Java编程思想(第4版) 中文清晰PDF完整版 [日期:2014-08-11] 来源:Linux社区 作者:Linux [字体:大 中 小] <Java编程思想>这本书赢得了全 ...
- nginx配置大全
nginx配置大全
- 一、Perfect Squares 完全平方数
一原题 Given a positive integer n, find the least number of perfect square numbers (, , , , ...) which ...
- 收缩VC数据库
注意: 在收缩日志前必须截断事务日志. 一. SQL Server 2008 收缩日志 (1) 使用SQL管理器收缩日志 第一步执行如下命令 ALTER DATABASE dbname SET REC ...
- [算法]打印N个数组的整体最大Top K
题目: 有N个长度不一的数组,所有的数组都是有序的,请从大到小打印这N个数组整体最大的前K个数. 例如: 输入含有N行元素的二维数组代表N个一维数组. 219,405,538,845,971 148, ...
- LINQ 学习路程 -- 查询操作 Join
Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...