4.9与类型相关的内置函数
(1)、string 函数
str.capitaze()字符串首字母大写
str.replace()
str.split()
(2)、序列处理函数
len()
max()
min()
其他
filter()
zip()
map()
reduce()
example4.9.1
(1)、string 函数
str.capitaze()字符串首字母大写
str.replace()
str.split()
(2)、序列处理函数
len()
max()
min()
其他
filter()
zip()
map()
reduce()
example4.9.1
>>> #str.capitaze()
>>> s ='hello world'
>>> capitalize()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
capitalize()
NameError: name 'capitalize' is not defined
>>> s.capitalize()
'Hello world'
example4.9.2
>>>#str.replace()
>>> s.replace('world','cuixiaohui')
'hello cuixiaohui'
>>> ## help(replace) 是错误的
>>> ## help(str.replace)是对的
>>> ss ='123123123'
>>> ss.replace('1','x')
'x23x23x23'
>>> ss.replace('1','X',1)
'X23123123'
>>> ss.replace('1','x',2)
'x23x23123'
>>> ss.replace('1','x',3)
'x23x23x23'
>>> list.append()
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
list.append()
TypeError: descriptor 'append' of 'list' object needs an argument
>>> help(list.append)
Help on method_descriptor:
append(...)
L.append(object) -> None -- append object to end
example4.9.3
>>> help(str.split)
Help on method_descriptor:
split(...)
S.split(sep=None, maxsplit=-1) -> list of strings
Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
>>> ip ='192.158.1.143'
>>> ip.split('.')
['192', '158', '1', '143']
>>> ip.split('.',1)
['192', '158.1.143']
>>> ip.split('.',2)
['192', '158', '1.143']
>>> ip.split('.',3)
['192', '158', '1', '143']
>>>
example4.9.4
#引入string模块,在python3.3.3未执行通,代码如下
>>> ss.replace('1','x',1)
'x23123123'
>>> s.replace('hello','good')
'good world'
>>> ip.split('.',3)
['192', '158', '1', '143']
>>> import string
>>> help(string.replace)
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
help(string.replace)
AttributeError: 'module' object has no attribute 'replace'
>>> help(sting.replace)
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
help(sting.replace)
NameError: name 'sting' is not defined
>>> string.replace(s,'hello','good')
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
string.replace(s,'hello','good')
AttributeError: 'module' object has no attribute 'replace'
>>>
example4.9.5
#序列处理函数
len()、max()、min()、其他
filter()、zip()、map()、reduce()
>>>#filter()举例子
>>> help(filter)
Help on class filter in module builtins:
class filter(object)
| filter(function or None, iterable) --> filter object
|
| Return an iterator yielding those items of iterable for which function(item)
| is true. If function is None, return the items that are true.
|
| Methods defined here:
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| __next__(...)
| x.__next__() <==> next(x)
|
| __reduce__(...)
| Return state information for pickling.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
>>> def f(x):
if x > 5:
return True
>>> f(10)
True
>>> l = range(10)
>>> l
range(0, 10)
>>> filter(f,l)
<filter object at 0x01F8FE30>
注:学习内容来源于网易云课堂《疯狂的Python:快速入门精讲》;代码执行环境为Win;Python版本为 3.3.3