1、rstrip()删除末尾指定字符串
例如:A = '1,2,3,4,5,'
B = A.rstrip(',')
B = '1,2,3,4,5'
2、isdigit()方法
Python isdigit() 方法检测字符串是否只由数字组成,返回 True 或者False。
例如:str = '123456'
str.isdigit()为True
3、enumerate() 函数
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
例如:seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
print i, element
输出:0 one
1 two
2 three
4、字符串 startswith、find、join
例如:name = 'Swaroop'
if name.startswith('Swa'): #startswith方法用于查找字符串是否以给定的字符串内容开头
print('the string start with "Swa"')
if 'a' in name:
print('it contains the string "a"')
if name.find('war') !=-1: #find方法用于定位字符串中给定字符串的位置,如果找不到对应的字符串,会返回-1
print('it contains the string "war"')
delimiter = '_*_'
mylist = ['Brazil','Russia','India','China']
print(delimiter.join(mylist)) #字符串delimiter将会作为每个项目之间的分隔,并以此返回一串更大的字符串
输出:the string start with "Swa"
it contains the string "a"
it contains the string "war"
Brazil_*_Russia_*_India_*_China
5、函数传递元组,返回多个变量
例如:def run():
return ('hello','world')
first, second = run()
print(first)
print(second)
输出:hello
world
6、在函数中接收元组与字典
分别使用 * 或 ** 作为元组或字典的前缀,来使它们作为一个参数为函数所接收
例如:def powersum(power, *args):
total = 0
for i in args:total += pow(i, power)
return total
print(powersum(2, 3, 4))
print(powersum(2, 10))
输出:25
100