python字符串方法
- 1,center()
s1 = "wangs" s2 = s1.center(15,'*') print (s2) s3 = len(s2) print(s3) *****wangs***** 15
- 2、 count()
s4 = "a is b are isaisa c" ll = len(s4) print(ll) s5 = s4.count('is') print(s5) 19 3
- 3 endswith()
s1 = "abc" s2 = s1.endswith('c') print(s2) True
- 4、expandtabs() 把tab键换成空格 ,可以自定义多少个空格
s1 = "hhh\tjjj" print(s1.expandtabs(20)) hhh jjj
- 5 find() 找符合的字符,返回找到的字符所在的位置
s1 = "ab is are" s2 = print(s1.find('a')) 0
- 6、format 字符串的格式化
s1 = "hello {0},age {1}" print(s1) new1=s1.format("aaa",19) print (new1) hello {0},age {1} hello aaa,age 19
- 7、isaloha 判断是不是全是字母
s1 = "88d88 " print(s1.isalpha()) False
- 8、 join 元素之间的分隔符是S
s1 = "wang" li = ["aaa","bbb"] print('S'.join(s1)) print(''.join(s1)) print(''.join(li)) wSaSnSg wang aaabbb
- 9 ljust 内容左边对齐 右边用 填充
- 10、####lstrip 移除左边的空格
s1 = " www 22 " print(s1.lstrip()) www 22
- 11、去掉右边的空格
s1 = " www 22 " print(s1.rstrip()) www 22
- 12、####去掉左边和右边的空格 中间的 去不掉
s1 = " www 22 " print(s1.strip()) www 22
- 13、partition 分割字符 用传参来分割成元祖类型
s1 = "ab is aa 3 fs " print(s1.partition('is') ('ab ', 'is', ' aa 3 fs ')
- 14、替换字符
s1 = "ab is aa" print (s1.replace('ab','xxx')) xxx is aa
- 15、##split 按照 什么来分割字符
s1 = "abisaaandandin" s2 = "aa\nbb\nadm" print(s1.split('and')) print(s2.split('\n')) ['abisaa', '', 'in'] ['aa', 'bb', 'adm']
- 16、## 大写变小写 小写变大写
s1 = "abdS" print (s1.swapcase()) ABDs