import模块:
randint(a,d)函数 : randint(a,d) 返回a,d之间的整数。
例:import.random
print(random.randint(0,9)) ##输出 :4
choice(seq)函数: 返回一个列表,元组或字符串的随机项。
例:import.random
print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9]) ##输出:choice([1, 2, 3, 5, 9]) : 2
print "choice('A String') : ", random.choice('A String') ##输出:choice('A String') : n
randrange([start],stop[step])函数: 返回指定递增基数集合中的一个随机数,基数缺省值为1。
例: import random
# 输出 100 <= number < 1000 间的偶数
print "randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2) ##输出:randrange(100, 1000, 2) : 976
# 输出 100 <= number < 1000 间的其他数
print "randrange(100, 1000, 3) : ", random.randrange(100, 1000, 3) ##输出:randrange(100, 1000, 3) : 520
random.shuffle(list)函数 : 将序列的所有元素随机排序。返回none
例:import random
list = [20, 16, 10, 5];
random.shuffle(list)
print "随机排序列表 : ", list ##输出:随机排序列表 : [16, 5, 10, 20]
random.shuffle(list)
字符串:
不可变,有序可迭代 用 ” 或 ” ” ,”’ ”’ 定义。
+ :合并两个字符串,产生临时字符串。
* : 复制填充元素,产生临时字符串。
r或R : 对转义符不做处理。
xxx.join() :将两个文件中,指定栏位内容相同的行连接起来。
例:'a'.join('1','2','3')
##输出 '1 a 2 a 3'
lstrip()函数:用于截掉字符串左边的空格或指定字符。
str.lstrip([chars]),chars --指定截取的字符。
str = " this is string example....wow!!! "; ### this is string example....wow!!!
print str.lstrip();
str = "88888888this is string example....wow!!!8888888"; ### this is string example....wow!!!8888888
print str.lstrip('8');
rstrip()函数: 删除 string 字符串末尾的指定字符(默认为空格)。
xxx.split(sep= maxsplit= ) :将字符串分割成若干列表;默认空格分隔符;返回新字符串。分隔符被切掉。
sep 指定分隔符。
maxsplit 指定次数,-1表示遍历字符串。
rsplit(): rsplit(sep= maxsplit= )从右开始分割。
例:tr = "Line1-abcdef \nLine2-abc \nLine4-abcd"
print str.split( ) ##'Line1-abcdef', 'Line2-abc', 'Line4-abcd']
print str.split(' ', 1 ) ##['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
splitlines():照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
例:str1 = 'ab c\n\nde fg\rkl\r\n'
print str1.splitlines(); ### ['ab c', '', 'de fg', 'kl']
str2 = 'ab c\n\nde fg\rkl\r\n'
print str2.splitlines(True) ### ['ab c\n', '\n', 'de fg\r', 'kl\r\n']
xxx.partition() :用来根据指定的分隔符将字符串进行分割。如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
例:str = "www.runoob.com"
print str.partition(".") #### ('www', '.', 'runoob.com')
rpartition() :目标字符串的末尾也就是右边开始搜索分割符 ;如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
例:str = "www.runoob.com"
print str.rpartition(".") #### ('www.runoob', '.', 'com')
replace(old new [count])函数 :方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数count,则替换不超过count 次。 返回新字符串,
sttrip ([chars]) : 两端去除chars指定字符,