python中强大的format函数

自python2.6开始,新增了一种格式化字符串的函数str.format(),此函数可以快速处理各种字符串。
语法

它通过{}和:来代替%。

请看下面的示例,基本上总结了format函数在python的中所有用法

 1 #通过位置
 2 print ‘{0},{1}‘.format(‘chuhao‘,20)
 3
 4 print ‘{},{}‘.format(‘chuhao‘,20)
 5
 6 print ‘{1},{0},{1}‘.format(‘chuhao‘,20)
 7
 8 #通过关键字参数
 9 print ‘{name},{age}‘.format(age=18,name=‘chuhao‘)
10
11 class Person:
12     def __init__(self,name,age):
13         self.name = name
14         self.age = age
15
16     def __str__(self):
17         return ‘This guy is {self.name},is {self.age} old‘.format(self=self)
18
19 print str(Person(‘chuhao‘,18))
20
21 #通过映射 list
22 a_list = [‘chuhao‘,20,‘china‘]
23 print ‘my name is {0[0]},from {0[2]},age is {0[1]}‘.format(a_list)
24 #my name is chuhao,from china,age is 20
25
26 #通过映射 dict
27 b_dict = {‘name‘:‘chuhao‘,‘age‘:20,‘province‘:‘shanxi‘}
28 print ‘my name is {name}, age is {age},from {province}‘.format(**b_dict)
29 #my name is chuhao, age is 20,from shanxi
30
31 #填充与对齐
32 print ‘{:>8}‘.format(‘189‘)
33 #     189
34 print ‘{:0>8}‘.format(‘189‘)
35 #00000189
36 print ‘{:a>8}‘.format(‘189‘)
37 #aaaaa189
38
39 #精度与类型f
40 #保留两位小数
41 print ‘{:.2f}‘.format(321.33345)
42 #321.33
43
44 #用来做金额的千位分隔符
45 print ‘{:,}‘.format(1234567890)
46 #1,234,567,890
47
48 #其他类型 主要就是进制了,b、d、o、x分别是二进制、十进制、八进制、十六进制。
49
50 print ‘{:b}‘.format(18) #二进制 10010
51 print ‘{:d}‘.format(18) #十进制 18
52 print ‘{:o}‘.format(18) #八进制 22
53 print ‘{:x}‘.format(18) #十六进制12

--End

时间: 2024-07-29 04:00:00

python中强大的format函数的相关文章

python 中的 str.format()函数

转自:http://blog.csdn.net/handsomekang/article/details/9183303 自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和:来代替%. "映射"示例 通过位置 In [1]: '{0},{1}'.format('kzc',18)   Out[1]: 'kzc,18'   In [2]: '

python中的内置函数getattr()

在python的官方文档中:getattr()的解释如下: getattr(object, name[, default]) Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For examp

函数式编程 & Python中的高阶函数map reduce filter 和sorted

1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数式编程的第一型.在面向对象编程中,我们把对象传来传去,那在函数式编程中,我们要做的是把函数传来传去,而这个,说成术语,我们把他叫做高阶函数.飞林沙 2)特点 计算视为视为函数而非指令 纯函数式编程:不需变量,无副作用,测试简单(每次的执行结果是一样的) 支持高阶函数,代码简洁 2. python支持

Python学习(二):入门篇:python中流程控制与函数编写

python中流程控制与函数编写 Last Eidt 2014/5/2 转载请注明出处http://blog.csdn.net/jxlijunhao 一,流程控制 1)布尔逻辑 Python中利用True来表示逻辑真,False来逻辑假 not :非 and:与 or   :或 ==  :逻辑等 >>> False==True False >>> False==False True >>> not False True >>> Fal

python中enumerate()函数用法

python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输出, 2.将 list 倒序成 [6, 5, 4, 3, 2, 1] 3.将a 中的偶数挑出 *2 ,结果为 [4, 8, 12] 这个例子用到了python中enumerate的用法.顺便说一下enumerate在for循环中得到计数的用法,enumerate参数为可遍历的变量,如 字符串,列表等

Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即 str()或者 repr() . >>> a = 10 >>> type(str(a)) <class 'str'> >>> type(repr(a)) <class 'str'> 但是这二者之间有什么区别呢?因

Python中进制转换函数的使用

Python中进制转换函数的使用 关于Python中几个进制转换的函数使用方法,做一个简单的使用方法的介绍,我们常用的进制转换函数常用的就是int()(其他进制转换到十进制).bin()(十进制转换到二进制).oct()(十进制转换到八进制).hex()(十进制转换到十六进制). 下面我们逐个说下每个函数的用法. bin bin()函数,是将十进制的数字转换成二进制的数字.其中bin()函数中传入的是十进制的数字,数据类型为数字类型. v = 18 num = bin(v) print(num)

Python 的格式化字符串format函数

阅读mattkang在csdn中的博客<飘逸的python - 增强的格式化字符串format函数>所做笔记 自从python2.6开始,新增了一种格式化字符串的函数str.format(),他通过{}和:来代替%. 1.映射实例 In[1]: '{0},{1}'.format('abc', 18) Out[1]: 'abc,18' In[2]: '{}'.format(18) out[2]: 18 In[3]: '{1},{0},{1}'.format('abc', 123) out[3]:

python 中的 re.compile 函数

正则表达式功能十分强大. “有些人面临一个问题时会想:‘我知道,可以用正则表达式来解决这个问题.’于是现在他们就有两个问题了”——Jamie Zawinski 同时正则表达式很难掌握. 正则表达式的各种规则就不在此赘述了,以下介绍在python的re模块中怎样应用正则表达式 1. 使用re.compile re模块中包含一个重要函数是compile(pattern [, flags]) ,该函数根据包含的正则表达式的字符串创建模式对象.可以实现更有效率的匹配.在直接使用字符串表示的正则表达式进行