python的内建函数的学习(一部分,并不是全部)
python的内建函数时相当方便,相当有用了,那是杠杠滴!!!
现在就把我所学的,在我理解的范围内记录下来,如果有不足或者错误之处,望发现的同仁及时之处哈!
python的内建函数主要有以下内容( Copied from www.python.org):
大约是80个内建函数,各自在不同的地方发挥重要作用。
在说内建函数时,一定要说python的数据类型,因为内建函数是针对不同数据类型的,有些函数在某一个数据类型中作用不大,但在其他的数据类型中可能有巨大作用。
python的标准类型包括:整型,长整型,浮点型,复数型,布尔型,字符串,列表,元祖,字典。简单点说,就是数值,字符串,序列,元祖,字典。
通用的内建函数:type(obj)返回obj的类型
str(obj)将obj转换为字符串
cmp(x,y)比较x和y, x>y 返回1,反正,返回-1,相等返回0。
Note:说他们通用是因为可以应用到所有的数据类型,可以将字符串转换为数字。Eg:cmp(0xAF,175),result:0
现在对不同类型的内建函数分别进行记录(不全哈)。
一、数值类型
1 Hex=hex(23) 2 print Hex 3 print type(Hex) 4 5 6 7 8 Result: 9 10 0X17 11 <type 'str'>
二、字符串
操作字符串的内建函数还是很多滴。
1.enumerate(str) 将字符串str中的值与对应的索引以元祖的方式一并返回。
Eg:
for i in enumerate([1,4,2,5,4]):
2.zip函数是对两个字符串,按位结合成元祖,最后得到序列。
3.raw_input(str),这个函数大家应该是最熟悉的了。
其他的chr,ord等等上面已做介绍。
Note:
下面是字符串的内建函数并不改变原字符串,只是得到新的字符串。
str='test string built-in-Function'
- 字符串长度获取:len(str)
例:print '%s length=%d' % (str,len(str)) - 字母处理
全部大写:str.upper()
全部小写:str.lower()
大小写互换:str.swapcase()
首字母大写,其余小写:str.capitalize()
首字母大写:str.title()
print '%s lower=%s' % (str,str.lower())
print '%s upper=%s' % (str,str.upper())
print '%s swapcase=%s' % (str,str.swapcase())
print '%s capitalize=%s' % (str,str.capitalize())
print '%s title=%s' % (str,str.title()) - str.decode(encoding='UTF-8')
-
格式化相关
获取固定长度,右对齐,左边不够用空格补齐:str.ljust(width)
获取固定长度,左对齐,右边不够用空格补齐:str.ljust(width)
获取固定长度,中间对齐,两边不够用空格补齐:str.ljust(width)
获取固定长度,右对齐,左边不足用0补齐
print '%s ljust=%s' % (str,str.ljust(20))
print '%s rjust=%s' % (str,str.rjust(20))
print '%s center=%s' % (str,str.center(20))
print '%s zfill=%s' % (str,str.zfill(20)) -
字符串搜索相关
搜索指定字符串,没有返回-1:str.find('t')
指定起始位置搜索:str.find('t',start)
指定起始及结束位置搜索:str.find('t',start,end)
从右边开始查找:str.rfind('t')
搜索到多少个指定字符串:str.count('t')
上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1
print '%s find nono=%d' % (str,str.find('nono'))
print '%s find t=%d' % (str,str.find('t'))
print '%s find t from %d=%d' % (str,1,str.find('t',1))
print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2))
#print '%s index nono ' % (str,str.index('nono',1,2))
print '%s rfind t=%d' % (str,str.rfind('t'))
print '%s count t=%d' % (str,str.count('t')) -
字符串替换相关
替换old为new:str.replace('old','new')
替换指定次数的old为new:str.replace('old','new',maxReplaceTimes)
print '%s replace t to *=%s' % (str,str.replace('t', '*'))
print '%s replace t to *=%s' % (str,str.replace('t', '*',1)) -
字符串去空格及去指定字符
去两边空格:str.strip()
去左空格:str.lstrip()
去右空格:str.rstrip()
去两边字符串:str.strip('d'),相应的也有lstrip,rstrip
str=' python String function '
print '%s strip=%s' % (str,str.strip())
str='python String function'
print '%s strip=%s' % (str,str.strip('d')) -
按指定字符分割字符串为数组:str.split(' ')
默认按空格分隔
str='a b c de'
print '%s strip=%s' % (str,str.split())
str='a-b-c-de'
print '%s strip=%s' % (str,str.split('-')) -
字符串判断相关
是否以start开头:str.startswith('start')
是否以end结尾:str.endswith('end')
是否全为字母或数字:str.isalnum() 至少有一个字符;
是否全字母:str.isalpha()
是否全数字:str.isdigit()
是否全小写:str.islower()
是否全大写:str.isupper()
str='python String function'
print '%s startwith t=%s' % (str,str.startswith('t'))
print '%s endwith d=%s' % (str,str.endswith('d'))
print '%s isalnum=%s' % (str,str.isalnum())
str='pythonStringfunction'
print '%s isalnum=%s' % (str,str.isalnum())
print '%s isalpha=%s' % (str,str.isalpha())
print '%s isupper=%s' % (str,str.isupper())
print '%s islower=%s' % (str,str.islower())
print '%s isdigit=%s' % (str,str.isdigit())
str='3423'
print '%s isdigit=%s' % (str,str.isdigit())
三、列表,元祖,字典
s=[6,2,5] L=s.sort() print L print s Result: None [2,5,6]
| D.copy( ) | Returns a (shallow) copy of the dictionary |
| D.has_key(k) | Returns True if k is a key in D, otherwise returns False |
| D.items( ) | Returns a copy of the list of all items (key/value pairs) in D |
| D.keys( ) | Returns a copy of the list of all keys in D |
| D.values( ) | Returns a copy of the list of all values in D |
| D.iteritems( ) | Returns an iterator on all items(key/value pairs) in D |
| D.iterkeys( ) | Returns an iterator on all keys in D |
| D.itervalues( ) | Returns an iterator on all values in D |
| D.get(k[,x]) | Returns an iterator on all values in D |
| Mutating methods | |
| D.clear( ) | Removes all items from D |
| D.update(D1) | For each k in D1, sets D[k] equal to D1[k] |
| D.setdefault(k[,x]) | Returns D[k] if k is a key in D; otherwise sets D[k] equal to x and returns x |
| D.popitem( ) | Removes and returns an arbitrary item (key/value pair) |
浙公网安备 33010602011771号