python 学习笔记---字符串
1.find 查找某个字符串是不是在指定的范围之内,在的话返回开始索引值,不在的话索引值是-1
str = "we are in the same school and we are in the same class"
In [2]: str.find("we")
Out[2]: 0
In [3]: str.find("your")
Out[3]: -1
2.index 方法和find一样,只是会出现一个异常
In [4]: str
Out[4]: 'we are in the same school and we are in the same class'
In [5]: str.index("we")
Out[5]: 0
In [6]: str.index("your")
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
----> 1 str.index("your")
ValueError: substring not found
3.count 计算一个字符串出现的次数
In [7]: str
Out[7]: 'we are in the same school and we are in the same class'
In [8]: str.count("we")
Out[8]: 2
4.replace 替换某个字符串,如果次数限定,那么替换不超过次数限定
In [7]: str
Out[7]: 'we are in the same school and we are in the same class'
In [8]: str.count("we")
Out[8]: 2
In [9]: str
Out[9]: 'we are in the same school and we are in the same class'
In [10]: str.replace("we","they")
Out[10]: 'they are in the same school and they are in the same class'
In [11]: str.replace("we","they",1)
Out[11]: 'they are in the same school and we are in the same class'
5.split 分隔切片字符串,如果有限定值,那么只分隔个子字符串
In [16]: str.split(" ")
Out[16]:
['we',
'are',
'in',
'the',
'same',
'school',
'and',
'we',
'are',
'in',
'the',
'same',
'class']
In [17]: str.split(" ",2)
Out[17]: ['we', 'are', 'in the same school and we are in the same class']
6.capitalize 大写字符串第一个字符
In [18]: str
Out[18]: 'we are in the same school and we are in the same class'
In [19]: str.capitalize()
Out[19]: 'We are in the same school and we are in the same class'
7.title 字符串每个字符首字符都大写
In [20]: str
Out[20]: 'we are in the same school and we are in the same class'
In [21]: str.title()
Out[21]: 'We Are In The Same School And We Are In The Same Class'
8.startswith 判断字符串是不是以某个字符开头,是就返回True,不是就返回False
In [22]: str
Out[22]: 'we are in the same school and we are in the same class'
In [23]: str.startswith("w")
Out[23]: True
9.endswith 判断字符串是不是以某个字符结尾,是就返回True,不是就返回False
In [27]: str
Out[27]: 'we are in the same school and we are in the same class'
In [28]: str.endswith("s")
Out[28]: True
10.lower 转换字符串中所有大写为小写
In [31]: str
Out[31]: 'We are in the same school and we are in the same class'
In [32]: str.lower()
Out[32]: 'we are in the same school and we are in the same class'
11.upper 转换字符串中所有小写为大写
In [33]: str
Out[33]: 'We are in the same school and we are in the same class'
In [34]: str.upper()
Out[34]: 'WE ARE IN THE SAME SCHOOL AND WE ARE IN THE SAME CLASS'
12.ljust 把一个字符串返回并左对齐,并用空格填充到指定宽度的新字符串
In [45]: str
Out[45]: 'We are in the same school and we are in the same class'
In [46]: str.ljust(60,"*")
Out[46]: 'We are in the same school and we are in the same class******'
In [47]: str.ljust(60)
Out[47]: 'We are in the same school and we are in the same class '
13.rjust 把一个字符串返回并右对齐,并用空格填充到指定宽度的新字符串
In [48]: str
Out[48]: 'We are in the same school and we are in the same class'
In [49]: str.rjust(60,"*")
Out[49]: '******We are in the same school and we are in the same class'
In [50]: str.rjust(60)
Out[50]: ' We are in the same school and we are in the same class'
14.center 把一个字符串返回并居中,并用空格填充到指定宽度的新字符串
In [51]: str
Out[51]: 'We are in the same school and we are in the same class'
In [52]: str.center(60,"*")
Out[52]: '***We are in the same school and we are in the same class***'
In [53]: str.center(60)
Out[53]: ' We are in the same school and we are in the same class '
15.lstrip 删除字符串左边空白字符
In [55]: str
Out[55]: ' We are in the same school and we are in the same class'
In [56]: str.lstrip()
Out[56]: 'We are in the same school and we are in the same class'
16.rstrip 删除字符串末尾空白字符
In [58]: str
Out[58]: 'We are in the same school and we are in the same class '
In [59]: str.rstrip()
Out[59]: 'We are in the same school and we are in the same class'
17.strip 删除字符串两端空白字符
In [61]: str
Out[61]: ' We are in the same school and we are in the same class '
In [62]: str.strip()
Out[62]: 'We are in the same school and we are in the same class'
18.partition 把字符串分成三部分
In [65]: str ="We are in the same school and we are in the same class"
In [66]: str
Out[66]: 'We are in the same school and we are in the same class'
In [67]: str.partition("school")
Out[67]: ('We are in the same ', 'school', ' and we are in the same class')
19.rpartition 把字符串从右边分成三部分
In [68]: str
Out[68]: 'We are in the same school and we are in the same class'
In [69]: str.rpartition("in")
Out[69]: ('We are in the same school and we are ', 'in', ' the same class')
20.splitlines 以行分隔,返回一个包含各行作为元素的列表
In [73]: str
Out[73]: 'We are in the same school\n and\n we are in the same class'
In [74]: str.splitlines()
Out[74]: ['We are in the same school', ' and', ' we are in the same class']
21.isalpha 判断一个字符串所有字符是不是都是字母,是就返回True,不是就返回False
In [79]: str
Out[79]: 'We are in the same school and we are in the same class'
In [80]: str.isalpha()
Out[80]: False
22.isdigit 判断一个字符串所有字符是不是都是数字,是就返回True,不是就返回False
In [81]: str = "1234567"
In [82]: str
Out[82]: '1234567'
In [83]: str.isdigit()
Out[83]: True
23.isalnum 判断一个字符串所有字符是不是都是字母或数字,是就返回True,不是就返回False
In [84]: str = "njsygxbc87654"
In [85]: str
Out[85]: 'njsygxbc87654'
In [86]: str.isalnum()
Out[86]: True
24.isspace 判断一个字符串所有字符是不是只有空格,是就返回True,不是就返回False
In [91]: str = "njsygxbc87654"
In [92]: str
Out[92]: 'njsygxbc87654'
In [93]: str.isspace()
Out[93]: False
In [94]: str = " "
In [95]: str
Out[95]: ' '
In [96]: str.isspace()
Out[96]: True
25.join 字符串所有字符后插入str,构造出一个新的字符串
In [97]: str = "we are in the same school and we are in the same class"
In [98]: str
Out[98]: 'we are in the same school and we are in the same class'
In [99]: str1 ="+"
In [100]: str1.join(str)
Out[100]: 'w+e+ +a+r+e+ +i+n+ +t+h+e+ +s+a+m+e+ +s+c+h+o+o+l+ +a+n+d+ +w+e+ +a+r+e+ +i+n+ +t+h+e+ +s+a+m+e+ +c+l+a+s+s'
In [101]: li =["a","b","c","d"]
In [102]: str1.join(li)
Out[102]: 'a+b+c+d'