您的位置:首页 > 编程语言 > Python开发

python3 字符串与列表常用功能

2016-04-13 18:39 585 查看
一、字符串常用功能

  1. capitalize(),将字符串的首字母变成大写,其余全部置为小写;如果字符串中有多个单词,也只是将第一个单词的首字母置为大写;例: 

>>> name = 'i am keVin ChOu'
>>> ret = name.capitalize()
>>> print(ret)
I am kevin chou


  2.casefold(),将字符串全部置为小写

>>> name = 'II am keVin ChOu'
>>> ret = name.casefold()
>>> print(ret)
ii am kevin chou


  3.center(),内容居于字符串总长度中间,其余部分用指定内容填充,默认无;其内部方法与例子如下:

#内部方法
def center(self, width, fillchar=None):

'''  内容居中,width:总长度;fillchar:空白处填充内容,默认无'''
return ""
#实例
>>> name = 'kevin'
>>> ret = name.center(20,'*')
>>> print(ret)
*******kevin********
>>>


  4.count(),统计子字符在指定范围内出现的次数,默认为整个字符串,也可以指定起始的索引范围;例:

>>> name = 'basketball'
>>> ret = name.count('a')
>>> print(ret)
2
>>> print(name.count('s'))
1
>>> print(name.count('a',0,5))
1


  5.endswith(),是不是已'xx'字符结束;startswith(),是不是以'xx'字符开始;两者都可以指定起始的索引范围。例: 

>>> name = 'My name is Kevin'
>>> name1 = 'My_Name_Is_Kevin'
>>> print(name.split())
['My', 'name', 'is', 'Kevin']
>>> print(name1.split('_'))
['My', 'Name', 'Is', 'Kevin']


View Code
   26.title() 把字符串中每个单词的首字母大写。例如:

>>> name = 'my name is Kevin'
>>> print(name.title())
My Name Is Kevin


  27.partition(),将字符串在指定分割符处分割成由前、中、后三个部分组成的元组

>>> name = 'basketball'
>>> print(name.partition('ke'))
('bas', 'ke', 'tball')


  28.format(),格式化字符串,例:

>>> str1 = 'my {0} {1} {username}.'
>>> print(str1.format('name','is',username='kevin'))
my name is kevin.


二、list 常用功能

  1.apend(),将元素添加进list中

>>> list1 = ['a','b','c','d','e']
>>> list1.append('f')
>>> print(list1)
['a', 'b', 'c', 'd', 'e', 'f']
>>>


  2.clear(),清空列表

>>> list1 = ['a','b','c','d','e']
>>> list1.clear()
>>> print(list1)
[]


  3.count(),统计列表中某个元素出现的次数

>>> list1 = ['a','b','c','d','a']
>>> print(list1.count('a'))
2


  4.extend(),用另一个list来扩充一个列表

>>> list1 = ['a','b']
>>> list2 = ['c','d','f']
>>> list1.extend(list2)
>>> print(list1)
['a', 'b', 'c', 'd', 'f']
>>>


  5.index(),找出指定范围内某个元素的索引位置,未找到抛出异常

>>> list1 = ['a','b','c','d','e']
>>> print(list1.index('b'))
1
>>> print(list1.index('f'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'f' is not in list


  6.insert(),在指定的索引位置,插入元素

>>> list1 = ['a','b','c','d','e']
>>> list1.insert(1,'kevin')
>>> print(list1)
['a', 'kevin', 'b', 'c', 'd', 'e']


  7.pop(),删除元素,并返回被删除的值,默认删除列表最后一个元素,也可以指定索引

>>> list1 = ['a','b','c','d','e']
>>> list1.pop()
'e'
>>> print(list1)
['a', 'b', 'c', 'd']
>>> list1.pop(0)
'a'
>>> print(list1)
['b', 'c', 'd']


  8.remove(),删除指定元素

>>> list1 = ['a','b','c','d','e']
>>> list1.remove('a')
>>> print(list1)
['b', 'c', 'd', 'e']


  9.reverse(),反转列表

>>> list1 = ['a','b','c','d','e']
>>> list1.reverse()
>>> print(list1)
['e', 'd', 'c', 'b', 'a']


  10.sort(),对列表进行排序,字符串跟数字不能直接进行排序

>>> list1 = ['a','d','b','c']
>>> list1.sort()
>>> print(list1)
['a', 'b', 'c', 'd']
>>>
>>> list2 = ['a',1,'2','d']
>>> list2.sort()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: