1. str.strip去掉字符串首位指定的字符,默认去掉空白字符
intput=" nnn入门小站nnn "print('[%s]' % input)# strip不会扭转原字符串的值,所以须要一个新变量接管newInput=input.strip()print('[%s]' % newInput)# 去掉首位的n字符print('[%s]' % input.strip('n'))# 去掉首位的n字符+空白字符print('[%s]' % input.strip('n').strip())
[ 入门小站 ][入门小站][ 入门小站 ][入门小站]
2. str.center应用指定字符对字符串进行对齐
intput="入门小站"print(input.center(12,'#'))
### 入门小站 ###
3. str.count()统计字符串再另外一个字符串中呈现的次数
# 语法 str.count(value, start, end) # start 起始索引 可选# end 完结索引 可选input="入门 入门小站 入门 rumen 入门"print(input.count('入门'))# 指定查找范畴print(input.count('入门',2,7))
41
4. str.find() 查找字符串首次呈现的地位
# 语法 str.find(value,start,end)# start 起始索引 可选# end 完结索引 可选# 如果没有找到则返回-1input="入门 入门小站 入门 rumen 入门"print(input.find('入门'))# 指定范畴print(input.find('入门',2,7))
03
5. str.rfind(value,start,end) 查找字符串最初呈现的地位
# 语法 str.rfind(value,start,end)# start 起始索引 可选# end 完结索引 可选# 如果没有找到则返回-1input="入门 入门小站 入门 rumen 入门"print(input.rfind('入门'))# 指定范畴print(input.rfind('入门',2,7))
173
6. str.swapcase()返回一个字符串的正本,并且对字符串进行大小写转换
input=" ru men XIAO zhan "print(input.swapcase())
RU MEN xiao ZHAN
7. str.startswith()判断字符串是不是以某个字符串开始
# 语法:str.startswith(prefix[, start[, end]]) -> bool# start 起始索引 可选# end 完结索引 可选# 返回bool值input="入门小站"print(input.startswith('入门'))# 指定范畴print(input.startswith('入门',2))
TrueFalse
8.str.endswith()判断字符串是不是以某个字符串结尾
# str.endswith(suffix[, start[, end]]) -> bool# start 起始索引 可选# end 完结索引 可选# 返回bool值input="入门小站"print(input.endswith('小站'))# 指定范畴print(input.endswith('小站',2))
TrueTrue
9. str.split() 宰割字符串成一个list,默认宰割符是任意数量的空白字符
# 语法:str.split(self, /, sep=None, maxsplit=-1)input=" n 入门 n 小站 n "print(input.split())# 指定宰割字符print(input.split('n'))
['n', '入门', 'n', '小站', 'n'][' ', ' 入门 ', ' 小站 ', ' ']
10. 字符串大小写转换
# 10.1 str.capitalize() 将字符串的第一个字符转成大写input="ru mEn z "print(input.capitalize())# 10.2 str.upper() 将所有字符转换成大写print(input.upper())# 10.3 str.title() 将每个单词的首字符转换成大写,其余字符转换成小写print(input.title())
Ru men z RU MEN Z Ru Men Z
11. str.ljust()和str.rjust()字符串首位用指定的字符填充到指定长度,默认以空格填充
# 语法: ljust(self, width, fillchar=' ', /)# 语法: rjust(self, width, fillchar=' ', /)# width 为填充后的字符串的长度input="rumenz"print(intput.ljust(20,'#'))print(input.rjust(20,'$'))
入门小站################$$$$$$$$$$$$$$rumenz
12. str.zfill() 字符串后面填充0
# 语法:str.zfill(self, width, /)# width 字符串填充后字符串的长度input="rumenz"print(intput.zfill(20))
0000000000000000入门小站
13. 定义一个多行字符串
input='''入门小站rumenz'''print(input)
入门小站rumenz
14. 判断字符串是否在另外一个字符串中
ints="ru men z "print('one' in ints)
False
15. is判断字符串地址时候雷同
str1="rumenz"str2="rumenz"print(str1 is str2)
True
16. 判断字符串的类型
# str.isalnum() 判断字符串是不是由字母和数字组成str1="rumenz123"print(str1.isalnum())# str.isalpha() 判断字符串是不是由字母组成print(str1.isalpha())# str.isdigit() 判断字符串是不是纯数字print(str1.isdigit())
TrueFalseFalse
17. 数组拼接成字符串
# 语法 "".join(str)li=['入门','小站','123']print(''.join(li))print('-'.join(li))
入门小站123入门-小站-123
18. 字符串替换
# 语法 str.replace(self, old, new, count=-1, /)# old 旧字符串# new 要替换成的新字符串# count 替换的次数,默认为-1 ,全副替换str1="入门小站入门"print(str1.replace("入门","rumen"))
rumen小站rumen
19. 字符串映射
# a->1 b->2 c->3 d->4 e->5 f->6p=str.maketrans('abcdef','123456')print('aaacccd'.translate(p))
1113334