python3 字符串与列表常用功能

时间:2023-10-16 11:59:38

一、字符串常用功能

  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 = 'basketball'
>>> print(name.endswith('l'))
True
>>> print(name.endswith('al'))
False
>>> print(name.endswith('e',0,4))
False
>>> print(name.endswith('e',0,5))
True
>>> print(name.startswith('b'))
True
>>> print(name.startswith('a',1,))
True
>>> print(name.startswith('a',2,6))
False
>>> print(name.startswith('as',0,6))
False
>>> print(name.startswith('as',1,6))
True

示例

  6.expandtabs(),将tab转换成空格,默认一个tab转换成8个空格;

>>> name = 'ke\tvin'
>>> ret = name.expandtabs()
>>> print(ret)
ke vin

  7.find() 返回子串在指定范围内首次出现的位置,未查到返回-1。例如:

>>> name = 'basketball'
>>> print(name.find('a'))
1
>>> print(name.find('x'))
-1
>>> print(name.find('b',1,))
6

   8.index()返回子串在指定范围内首次出现的位置,未查到抛出异常。例如:

>>> name = 'basketball'
>>> print(name.index('a'))
1
>>> print(name.index('x'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> print(name.index('b',1,))
6

  9.isalnum()判断字符串是否全是字母和数字(要么全是字母,要么全是数字,要么全是数字和字母)例如:

>>> str1 = 'x5y'
>>> str2 = 'ab4$'
>>> print(str1.isalnum())
True
>>> print(str2.isalnum())
False

  10.isalpha()方法判断字符串内容全是字母。例如:

>>> str1 = 'Myname'
>>> str2 = 'myageis25'
>>> str3 = 'my name'
>>> print(str1.isalpha())
True
>>> print(str2.isalpha())
False
>>> print(str3.isalpha())
False

  11.isdecimal()和isnumeric()判断字符串是否全是数字,该字符串必须是unicode object。例如:

>>> str1 = u'123456'
>>> str2 = u'myageis25'
>>> print(str1.isdecimal())
True
>>> print(str2.isdecimal())
False

  12.isdigit()判断字符串全部为数字。例如:

>>> str1 = '123456'
>>> str2 = 'myageis25'
>>> print(str1.isdigit())
True
>>> print(str2.isdigit())
False

  13.islower()判断字符串中所有的字母是否都是小写。 isupper() 判断字符串中所有的字母是否都是大写。例如:

>>> str1 = "THIS is string example....wow!!!";
>>> print( str1.islower());
False
>>> str2 = "this is string example....wow!!!";
>>> print( str2.islower());
True

  14.isspace()判断字符串是否全是空白符,例如:

>>> str1 = "\t\n\r"
>>> print(str1.isspace())
True
>>> str2 = "this "
>>> print(str2.isspace())
False

  15.istitle()判断字符串中,每个单词的首字母是否都是大写。例如:

>>> name = 'My name is Kevin'
>>> name1 = 'My Name Is Kevin '
>>> print(name.istitle())
False
>>> print(name1.istitle())
True

   16.join()通过特殊字符把字符串连接起来,例如:

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

>>> 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()