01.字符串常用方法
1.1 find
方法
作用:
find方法可以在一个较长的字符串中查找子串,他返回子串所在位置的最左端索引,如果没有找到则返回-1
1 | a = 'abcdefghijk' |
1.2 join
方法
作用:
join方法是非常重要的字符串方法,他是split方法的逆方法,用来连接序列中的元素,并且需要被连接的元素都必须是字符串。
1 | a = ['1','2','3'] |
1.3 split
方法
作用:
这是一个非常重要的字符串,它是join的逆方法,用来将字符串分割成序列
1 | print('1+2+3+4'.split('+')) #the result : ['1', '2', '3', '4'] |
1.4 strip
作用:
strip 方法返回去除首位空格(不包括内部)的字符串
1 | print(" test test ".strip()) #the result :“test test” |
1.5 replace
作用:
replace方法返回某字符串所有匹配项均被替换之后得到字符串
1 | print("This is a test".replace('is','is_test')) #the result : This_test is_test a test |
1.6 首字母大写
1 | 'aBdkndfkFFD' s = |
1.7 Pinyin 模块,将汉字转换成拼音
1 | #! /usr/bin/env python |
02.字符串格式化
2.1 使用百分号(%)字符串格式化
1 | num = 100 |
2.2 使用format字符串格式化
1 | #1. 位置参数 |
__END__