对象通常都拥有属于自己的方法(英文叫method)。对象的方法其实可以看成是对象所拥有的函数。也就是说这个方法,是属于这个对象的函数。调用对象的方法,和调用函数差不多,只要在前面加上所属对象和一个点。
字符串的方法
###### count
count 方法可以返回字符串对象包含了**多少个参数指定的字符串**。
```python
a = 'ffffgggh'
b = a.count('f')
print(b)
```
###### find
find 方法会在字符串中**查找参数子字符串**,并返回该参数字符串在其中第一个出现的位置索引
```python
a = 'ffffgggh'
c = a.find('h')
print(c)
```
如果字符串对象里面没有查找的参数字符串,就会返回-1。
find方法的第二个参数,用来指定查找字符串范围。
```python
a = 'ffffgggh'
c = a.find('h',5)
print(c)
```
###### split,splitlines
split是我们常用的方法,经常用来从字符串中**截取出我们想要的信息**。
split方法以参数字符串为分割符,将字符串 **切割为多个字符串**,作为元素存入一个列表,并返回这个列表。
```python
url ='https://www.hao123.com/?tn=85070231_59_hao_pg'
u = url.split('/')
print(u)
```
字符串对象还有一个splitlines方法也比较常用,就是把字符串按换行符进行切割。
```python
url ='https://www.hao123.com/?tn=85070231_59_hao_pg'
u = url.splitlines()
print(u)
```
splitlines方法经常用在文件处理中,读出文件内容存入字符串,然后调用字符串的。
splitlines方法,把每行内容依次存入列表。
###### join
join是将列表中的字符串元素,**以某字符串为连接符,连接为一个字符串。**
```python
url ='https://www.hao123.com/?tn=85070231_59_hao_pg'
u = url.split('/')
print('split:',u)
j = '/'.join(u)
print('join:',j)
```
###### strip,lstrip,rstrip
strip方法可以将字符串**前面和后面的空格删除**,但是不会删除字符串中间的空格。
```python
a = ' he llo '
b = a.strip()
print(b)
```
lstrip 方法将字符串前面(左边)的空格删除,但是**不会删除字符串中间和右边的空格。**
```python
a = ' he llo '
b = a.strip()
c = a.lstrip()
print(b)
print(c)
```
rstrip 方法将字符串后面(右边)的空格删除,但是**不会删除字符串中间和左边的空格。**
```python
a = ' he llo '
b = a.strip()
c = a.lstrip()
d = a.rstrip()
print(b)
print(c)
print(d)
```
###### replace
replace 也常用的方法,用来**替换**字符串里面所有指定的字符串为另一个字符串。
```python
url = "https://www.hao123.com/?tn=85070231_59_hao_pg"
urlTest = url.replace('https','http')
print(urlTest)
```
###### startswith和endswith
startswith方法检查字符串是否以参数指定的字符串**开头**,如果是,返回True,否则返回False。
endswith方法检查字符串是否以指定的字符串**结尾**,如果是,返回True,否则返回False。
###### isdigit
isdigit 方法检查**字符是否全部数字构成**,如果是,返回True,否则返回False。
```python
str1 = input('请输入手机号码:')
if not str1.isdigit(): # 不是全部由数字字符构成
print('您输入的手机号码不正确,必须全部是数字')
```
###### 字符串的倒序
要得到一个字符串的倒序字符串,只需要使用切片操作 **\[:: -1\]**
:: 表示切片字符串的从头到尾,也就是全部内容,而步长为-1表示,颠倒过来取元素。
```python
str1 = '明天见面'
str2 = str1[::-1]
print(str2)
```
列表的方法
###### append
append 方法就会改变列表的内容,**在后面添加一个元素**。
```python
list1 = [1,2,3,4,'hello']
list1.append('你好')
print(list1)
```
\*\*注意:\*\*append方法的返回值是None,而不是新的列表对象。
###### insert
如果我们不是要在后面添加一个元素,而是**在指定位置插入一个元素**,就可以使用insert方法。
```python
list1 = [1,2,3,4,'hello']
list1.insert(2,'哈喽')
print(list1)
```
同样,insert方法的返回值也是None。
###### pop
如果我们要从列表**取出并删除**一个元素,就可以使用pop方法。
该方法的参数就是要取出的元素的索引。
**注意:**取出后,该元素就从列表中删除了,所以pop也**经常用来删除某个元素。**
```python
list1 = [1,2,3,4,'hello']
list1.insert(2,'哈喽')
list1.pop(2)
print(list1)
```
pop 方法的返回值 是 提取出来的元素。
###### remove
remove 方法也是删除列表元素。
pop方法的参数是要删除元素的索引,而remove方法的参数就是要删除元素的值。
remove 从第1个元素开始,寻找和参数对象相等的元素,如果找到了,就删除。找到后,不会继续往后寻找其它相等的元素。
也就是说remove最多只会删除1个元素。
```python
list1 = [1,2,3,4,'hello']
list1.insert(2,'哈喽')
list1.remove('hello')
print(list1)
```
同样的,remove方法的返回值也是None。
###### reverse
reverse 方法**将列表元素倒过来。**
```python
list1 = [1,2,3,4,'hello']
list1.insert(2,'哈喽')
list1.remove('hello')
list1.reverse()
print(list1)
```
同样的,reverse 方法的返回值也是None
###### index
index 方法**返回** 参数对象在列表中的位置,也就是**索引**。
```python
list1 = [1,2,3,4,'hello']
list1.insert(2,'哈喽')
list1.remove('hello')
list1.reverse()
idx = list1.index(3)
print(idx)
print(list1)
```
###### sort
可以使用sort方法对列表进行排序。
```python
students = ['Alex','Tom','Jerry','Michale','Alex']
students.sort()
print(f'after sort: {students}')
numbers = [7,3,8,2,9]
numbers.sort()
print(f'after sort: {numbers}')
```
\*\*注意:\*\*调用过sort方法后,列表中的内容就改变了。
**剩下的内容,我会继续发布,耐心等一下哦\~,各位对学习的热情是我创作的动力。**