字符串方法

字符串方法

1.find()方法

str.find(str, beg = 0, end = len(string))

此语法中,str代表指定检索的字符串,beg代表开始索引,默认为0,end代表结束索引,默认为字符串的长度。返回结果为子串所在位置的最左端索引,如果没找到,就放回-1.

>>>field = 'do it now'

>>>field.find('do')
0
>>>field.find('now')
6
>>>field.find('python')
-1

find方法还可以接受参数,用于表示起始点和结束点

>>>field.find('it', 2)  #提供起点
3
>>>field.find('it', 5)
-1
>>>field.find('it', 0, 3)   #提供起点和终点
-1
>>>field.find('it', 0, 5)
3
>>>field.find('it', 5, 10)
-1

由输出结果看到,可以通过使用起始值和终止值查找指定的范围内是否存在指定字符串。

2.join()方法

str.join(sequence)

join()方法用于将序列终点元素以指定字符连接成一个新字符串。

>>>num = [1, 2, 3, 4]
>>>mark = '+'
>>>mark.join(num)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int found
>>>num.join(mark)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'join'
>>>field = ['1', '2', '3', '4', ‘5’]
>>>print('链接字符串列表:', mark.join(field))
链接字符串列表:1+2+3+4+5
>>>field.join(mark)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'join'
>>>dirs = '', 'home', 'data', 'hfs'
>>>print('路径:', '/', join(dirs))
路径:/home/data/hdfs

由输出结果看到,进行join操作时,调用和被调用的对象都必须时字符串,任意一个不是字符串都会报错。

3.lower()方法

lower()方法用于将字符串中所有大写字符串转换为小写。

str.lower()

>>>field = 'DO IT NOW'
>>>print('调用lower得到字符串:',field.lower())
调用lower得到字符串:do it now
>>>field = 'Hello World'
>>>print(''调用lower得到字符:串, field.lower())
调用lower得到字符串:hello world

 

4.upper()方法

upper()方法用于将字符串中的小写字母转换为大写字母。

str.upper()

此语法中,不需要参数。

>>>field = 'do it now'
>>>print('调用upper得到字符串:',field.upper())
调用upper得到字符串:DO IT NOW

>>>field = 'do it now'
>>>field.find('It') # 都不转换为大写,找不到匹配字符串
-1
>>>field.upper().find('It') # 被查找字符串不转换为大写,找不到匹配字符串
-1
>>>field.upper().find('It'.upper()) #使用upper方法转换为大写后查找
3

# 由输出结果看到,字符串全部转换为大写后能找到对应子串

 

5.swapcase()方法

swapcase()方法用于对字符串的大小写字母进行转换,将字符串中的大写字母转换为小写、小写字母转换为大写。

>>>field = 'Just do it, NOW'
>>>print('调用swapcase方法后得到的字符串:',field.swapcase())
调用swapcase方法后得到的字符串:jUST DO IT, now

6.replace()方法

replace()方法用于把字符串中的old(旧字符串)替换成new(新字符串),如果指定第3个参数max, 替换次数不超过max次

str.replace(old, new[, max])

 

>>>field = 'do it new ,do right now'
>>>print('新字符串:',field.replace('do', 'Just do'))
新字符串:Just do it now, Just do right now
>>>print('新字符串:',field.replace('o', 'Just', 1))
新字符串:dJust it now, do right now
>>>print('新字符串:',field.replace('o', 'Just', 2))
新字符串:dJust it now, dJust right now

 

  

 

7.split()方法

split()方法通过指定分隔符对字符串进行切片,如果num由指定值,就之分割num个子字符串,join的逆方法。

str.split(st='', num = string.count(str))

如果不提供分割符,程序就会把所有空格作为分隔符。

8.strip()方法

strip()方法用于自出字符串头尾指定的字符串(默认为空格)。

str.strip([chars])

strip方法只能取出头尾匹配的字符,中间匹配的字符串不会去除。

9.translate()方法

 translate()方法根据参数table给出的表(包含256个字符)转换字符串的字符,将要过滤的字符放到del参数中。

str.translate(table[, dletechars])

1 intab = 'adefs'
2 outtab = '12345'
3 trantab = str.maketrans(intab, outtab)
4 st = 'just do it'
5 print('st调用translate方法后:',st.translate(trantab))

st调用translate方法后:ju5t 2o it

translate方法和replace方法一样,可以替换字符串中某些部分。和replace方法不同的是,translate方法只处理单个字符。translate方法的优势在于可以同时进行多个替换,有时比replace方法效率高得多。

posted on 2020-06-29 19:08    阅读(217)  评论(0)    收藏  举报

导航