天天看点

Python 方法

<1> index()

在列表中查找值的下标,如果不存在,返回ValueError

如果有重复的值,就返回第一次出现的下标

spam = ['a','b','c']

spam.index('a')

返回0

<2> append()

Python 方法

只能将参数添加到列表末尾

<3>insert()

Python 方法

在某一下标处,插入数据

<4>del()

Python 方法

删除某下标的值

<5>remove()

Python 方法

删除某个值

<6>sort()

Python 方法

对列表中的数值进行排序,排序是逆序

对列表中的字符串按照ascii码排序

sort(str.lower)表示安装普通字典排序

<7>3个字典方法 keys()、values()、items()

spam = {'name':'kaka','age':'37'}

Python 方法

<8>get()方法

默认值为0

Python 方法

spam.get相当于取某个key的value

<9> setdefault()方法

确保一个键存在,默认是0

Python 方法

结果:

Python 方法

<10>upper() lower()

upper()是将原来的字符串转换为大写

lower()是将原来的字符串转换为小写

Python 方法
Python 方法

<11>

isupper() 表示字符串都是大写

islower() 表示字符串都是小写

isalpha() 表示字符串只包含字母,并且不为空

isalnum() 表示字符串只包含字母和数字,并且不为空

isdecimal() 表示字符串只包含数字字符,并且不为空

isspace() 表示字符串只包含空格、制表符和换行,并且不为空

istitle() 以大写字母开头,后面都是小写字母的单词

<12> startswith() endswith()

'Hello world'.startswith('Hello') 返回为True 表示以Hello为开头,那么endswith() 表示以什么字符串为结尾

<13> join() 和 split()

join() 是把列表中的字符串连接成新的字符串

Python 方法

split()是将字符串拆分成列表

Python 方法
Python 方法

<14>rjust() 右对齐

ljust() 左对齐

center() 中间对齐

Python 方法

<15>删除空白字符

Python 方法

本文转自大角牛博客51CTO博客,原文链接http://blog.51cto.com/jingshengsun888/2052048如需转载请自行联系原作者

运维的戏子