移除空白 .strip()
分割 .split() 传入一个字符串 以字符串为分割点, 返回一个列表
长度 len(obj)
索引 obj[1]
切片 ojb[:1], obj[1:10], obj[1:]
e.g.
name = 'eric'
print type(name)
(type 'str')
print dir(name)
显示 name 这个变量下面所有方法, 在这里列子里面是显示所有的字符串方法
def __contains__(self):
pass
检查这个值是否含有实参这个值, 如果有返回TRUE, 如果没有返回FALSE
def __getattribute__(self, *args, **kwargs):
'''Return getattr(self.name):
pass
反射的时候会用到
def capitalize (self): #首字母大写
pass
def casefold(self): #首字母小写
pass
def center(self, with,fillcharct)L
pass
e.g. result = name.center(20, "*")
print result
def count(self, sub, start=None, end=None):
"""子序列个数 """
"""S.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are interpreted
as in slice notation.
"""
return0
e.g.
name = "abcdefghijklmnopq'
def encode(self, encoding = 'utf-8',erros = 'strict')
pass
name = '李杰‘
res = name.encode('gbk')
print res
def endswith(self, suffix, start=None, end=None): # 以什么为结尾 /// def endswith(self, suffix, start=None, end=None): #以什么为开头
def expandtabs(self): TAB键值转换空格
name = 'alex'
res = name.expandtabs()
print res
def find(self, sub, start = None, end=None):
pass
def __format__(self): ##拼接
pass
def join(self,iterable ): ##拼接] 传入可以迭代的实参
pass
e.g.
li = ['s', 'b', 'i', 's', 'a', 'l', 'e', 'x']
def ljust(self, witdh, fillchar = None): #左对齐
pass
def rjust(self, width, fillchar = None): # 右对齐
pass
def lower(self): ##全部改成小写
pass
def upper(self): ##全部改成大写
pass
def translate(self, table, deletechars=None):
"""转换,需要先做一个对应表,最后一个表示删除字符集合
intab = "aeiou"outtab = "12345"trantab =maketrans(intab, outtab)
str = "this is string example....wow!!!"
print str.translate(trantab, 'xm')
"""
"""S.translate(table [,deletechars]) ->string
Return a copy of the string S, where all characters occurring
in the optional argument deletechars are removed, andthe
remaining characters have been mapped through the given
translation table, which must be a string of length 256 orNone.
If the table argument is None, no translation is applied andthe operation simply removes the characters indeletechars.
"""return ""
def index(self, sub, start=None, end=None):
"""子序列位置,如果没找到,报错 """S.index(sub [,start [,end]]) ->int
Like S.find() but raise ValueError when the substring is notfound.
"""return 0
def partition(self, str):
pass
def replace(self, old, new, count=None):
""" 替换 """
"""S.replace(old, new[, count]) ->string
Return a copy of string S with all occurrences of substring
old replaced by new. If the optional argument count isgiven, only the first count occurrences are replaced.
"""return ""
def split(self, sep=None, maxsplit=None):
pass
def split(self, sep=None, maxsplit=None):
""" 分割, maxsplit最多分割几次 """
"""S.split([sep [,maxsplit]]) ->list of strings
Return a list of the words inthe string S, using sep as the
delimiter string. If maxsplit isgiven, at most maxsplit
splits are done. If sep is not specified or isNone, any
whitespace string is a separator andempty strings are removed
fromthe result.
"""return []
pass
3. 列表 list []
存放集合/可变元素的集合
定义列表的二种方式
li = [1,2,3]
li = list((1,2,3))
list ----> 创建列表,将其他元素转换成 列表
1.创建
2.练习 字符, 字符串转换成列表
公共功能
索引 index
切片 []
长度 len
for ... in loop /// while
emmurate
del/
in 包含
特有功能
def index(self, value, start = None, stop = None):
return
#获取下标(索引)
def append(self, value):
pass
def insert(self, inex, p_object): #插入指定位置
pass
def pop(self, index = None):
return
#删除指定位置的元素并返回这个元素
def remove(self, value):
pass
删除指定的元素且是列表中的第一个
def reverse(self):
pass
#反转列表
def sort(): #排序
pass
def sort(self, cmp=None, key=None, reverse=False): #real signature unknown; restored from __doc__
"""L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1
"""
pass
def del(self,value):
pass
元祖 tuple
tu = (11, 22, 33,44)
tu = tuple((11,22,33,44))
共有功能
count
index
嵌套(元素不可以被修改)
def count(self, value):
计算元素出现的个数
def index(self, value)
获取元素的 索引位置
字典
dict
dic = {'k1': 'v1', 'k2': 'v2'}
dic = dict(k1 = 'v1', k2 ='v2'}
共有功能
索引
新增 dict[key] = 'str'
删除 del d[key]
keys, values, items 键,值,键值对
for k, v in dict.items: 返回列表(键值对)元组
def __init__(self, seq=None, **kwargs):
如果 K键不在, 可以指定一个V值,或默认NONE
def get(self,k,d=None):
e.g. dic = {'k1': 'v1', 'k2': 'v2'}
print dic['k1']
print dic['k2']
dic.get('k1')
dic.get('k2')
dic.get('k3') #如果不指定泽自动指定一个NONE值给 'k3'
dic.get('k3', 'v3')
print dic
def has_key(self, k): # 查看字典内是否有这个KEY键值 返回布尔值
pass
def items(self):
#获取所有的键和阀值, k,v 并以列表的形式返回
def keys(self):
#获取所有的键值, k 并以列表的形式返回
def values(self):
#获取所有的阀值 , v 并以列表的形式返回
e.g.
dic = {'k1': 'v1', 'k2': 'v2'}
print dic.keys() # 返回所有键值的列表
print dic.values() # 返回所有阀值的列表
print dic.items() # 返回所有键和阀值的列表
for k in dic.keys():
print k
for k in dic:
print k
for v in dic.values():
print v
for k,v in dic,items():
print k, v
def pop(self, k, d=None): #real signature unknown; restored from __doc__
"""获取并在字典中移除 """
"""D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
"""
pass
# 并且返回一个值
def update(self, E=None, **F): #known special case of dict.update
"""更新
{'name':'alex', 'age': 18000}
[('name','sbsbsb'),]
"""
"""D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
If E present and has a .keys() method, does: for k in E: D[k] = E[k]
If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]
"""
pass
def fromkeys(S, v=None): #real signature unknown; restored from __doc__
"""dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
v defaults to None.
"""
pass
def clear(self): #real signature unknown; restored from __doc__
"""清除内容 """ """D.clear() -> None. Remove all items from D. """ pass
def enumerate(self): 字典方法, 将一个元祖 作为阀值, 设置一个KEY(整数), 返回一个字典
pass #