Python3 字符串与列表常用功能(2)

>>> list1 = ['k','e','v','i','n']
>>> ret = ''.join(list1)
>>> ret1 = '*'.join(list1)
>>> print(ret)
kevin
>>> print(ret1)
k*e*v*i*n
>>> print('_'.join(('a','b','c',)))
a_b_c

17.len(str) 计算字符串的长度。

>>> str1 = 'kevin'
>>> print(len(str1))
5

18.str.lower()把所有的大写字母转成小写;str.upper()把所有的小写字母转成大写;swapcase() 方法是把字符串中的小写转成大写,大写转成小写。例如:

>>> name = 'kevin'
>>> print(name.lower())
kevin
>>> print(name.upper())
kevin
>>> print(name.swapcase())
kevin

19.lstrip()去除掉字符串左边规定的字符,默认是空格;rstrip()去除掉字符串右边规定的字符,默认是空格;strip()去除掉两边规定的字符,默认是空格。例:

>>> name = '**kevin**'
>>> print(name.rstrip('*'))
**kevin
>>> print(name.lstrip('*'))
kevin**
>>> print(name.strip('*'))
kevin

20.maketrans(),translate() 例:例子中实际上是把对应的字母替换成数字。

>>> str1 = 'my name is kevin'
>>> str_tab = str1.maketrans('mya','137')
>>> print(str1.translate(str_tab))
13 n71e is kevin

21.max()返回字符串中最大的字母。例如:

>>> str1 = "this is really a string example....wow!!!" 
>>> print(max(str1))
y

22.replace()用新字符替换旧字符,str.replace(old,new[, max]) max表示替换的个数

>>> str1 = "this is really a string example....wow!!!" 
>>> print(str1.replace('is','are'))
thare are really a string example....wow!!!
>>> print(str1.replace('is','are',1))
thare is really a string example....wow!!!  

23.rfind(),从字符串右边开始查找,返回指定范围内,子串最后出现的索引,找不到返回-1。例如:

>>> name = 'basketball'
>>> print(name.rfind('a'))
7
>>> print(name.rfind('a',0,5))
1

24.zfill(),用“0”从左边进行填充。例:

>>> name = 'basketball'
>>> print(name.zfill(15))   
00000basketball

25.split()按指定的分隔符分隔字符串,最终返回一个列表。例如:

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

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(),删除元素,并返回被删除的值,默认删除列表最后一个元素,也可以指定索引

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/fa97e7463af6e71968e2e7f51adcd2e0.html