字符串内置函数–capitalize 遇见非字符串后,下一个字符大写
-
str.capitalize()
:将字符串转换成大写,其他字母变成小写 -
capitalize 含义
capitalize(...)
| S.capitalize() -> str #输出是一个字符串
|
| Return a capitalized version of S, i.e. make the
first character have upper case and the rest lower case.
#返回一个首字母大写的字符串,即让第一个字母大写,其余字母小写
- 首字符会转换成大写,其余的都会转换成小写,
针对的就是第一个字符而言
- 首字符如果是非字母,则保持不变,并且其余的都会变成小写
# capitalize 如何查 数据类型的方法
print('helloWorld111'.capitalize())# Helloworld111
print('Hello World 123'.capitalize()) # 不是单词第一个大写哦
print('1HelloWowW'.capitalize())#1hellowoww 起始是数字也不行 就是默认第一位。
print('~!heEW'.capitalize())
字符串内置函数–center 填充内容使得内容居中显示
-
str.center(width[,fillchar])
:定义一个width宽度的字符串,使数据居中显示,其余部分使用fillchar进行填充,如果未定义,默认为空格填充。 -
参数至少为一个,最多为两个,
str.center()
会报错TypeError: center() takes at least 1 argument (0 given)
-
会在width中居中显示,如果不是居中,那么前短后长哦~
-
fillchar 默认是
空格
,如果有参数,只能是一个字符
,不然会报错The fill character must be exactly one character long
-
如果
width<str.len
会原样输出,并且也不会填充,也不会截取 -
help 输出的内容:
center(...)
| S.center(width[, fillchar]) -> str # 输出是一个字符串
| #s.center(宽度,填充字符) 填充字符是可选的吧
| Return S centered in a string of length width. Padding is
| done using the specified fill character (default is a space)
# fillchar
print('111111111111111111111111')#24
print('hello'.center(24))
print('hello'.center(3,'1')) # hello
print('hello'.center(10,'-'))
print('hello'.center(10,'*'))
#print('hello'.center(10,'ss')) #The fill character must be exactly one character long
字符串内置函数–count() 计算在一定范围内,字符串出现的个数
str.count(sub,start,end)
,搜索sub字符串在str字符串(statr,end)之间重复出现的个数
start,end是可选参数,默认是statr=0,end=len(str)- sub字符的个数是没有限制的,必须是完全一样的才算是出现的个数 否则不算哦
start,end
是左闭右开
的取值方式哦!也就是左边start的取值是可以取值的,但是end的取值不行- 是否也有负数的statr,end
- 如果不想填start好像不能省略,会报错
invalid syntax
- start,end的取值要求与索引一样,与之前的切片取值是一样的哦!
- help的解释
count(...)
| S.count(sub[, start[, end]]) -> int # 输出是一个整形
|
| Return the number of non-overlapping occurrences of substring sub in string S[start:end].
# 返回字符串中出现字串 出现的次数
Optional arguments start and end are interpreted as in slice notation.
#可选参数start end 被解释为切片符号,也就是起始和结束
|
- 验证代码
# count
counts = '01234567891111222233333444dddasabcd'
print(counts.count('1')) #5 0 - len(str)
print(counts.count('0',1))#0
print(counts.count('1',0,1)) #0 end 不取
print(counts.count('1',1,2))# 1 statr 取
print(counts.count('aa')) #0
print(counts.count('asa'))#
print(counts.count('asaaa'))#0
print(counts.count('a',0,-2))#2
print(counts.count('a',0,-4))#-1
字符串内置函数–bytes.decode()/str.encode 字符的编码和解码
- 含义:以指定的编码格式解码byte对象,默认‘utf-8’
bytes.decode('encodeing="utf-8",errors'strict'')
可以直接写等号右边的值- encodeing 是使用的编码,errors是设置不同错误的处理方式
默认为 ‘strict’,意为编码错误引起一个UnicodeError。 其他可能得值有 ‘ignore’, ‘replace’,
‘xmlcharrefreplace’, ‘backslashreplace’ 以及通过 codecs.register_error()
注册的任何值。
- 使用decode解码之前 都是要先编码的
str.encode('encoding')
,因为decode是针对byte对象进行处理的,所以要先转换成byte对象。否者报错'str' object has no attribute 'decode'
str.encode
是对字符串进行编码,注意字符串是要进行编码之后才可以进行解码。
str = '这是'
enstr= str.encode('utf-8')
print(str.encode('utf-8')) #b'\xe8\xbf\x99\xe6\x98\xaf'
print(enstr.decode('utf-8')) # 这是
字符串内置函数–endswith()/startswidth() 在特定范围内,检测是否以特定字符开头或结尾
str.endswith(suffix,start,end)
检测在start,end范围内,字符串是否以suffix内容作为结尾,返回的是一个bool布尔值True/False。
- start,end是切片位置,左闭右开 (取值范围),end是取不到的
- suffix的取值类型:suffix 必须是str,或者是只含有字符串的元祖
endswith first arg must be str or a tuple of str, not int
- start,end 的默认情况 ,是否可以省略
str.startswith(suffix,start,end)
,用法相同,检测是否以规定的子字符串开头,返回True/False
endswith(...) # 字符串字段以这个结尾
| S.endswith(suffix[, start[, end]]) -> bool
| # s.endswith('可以理解为内容',statr,end)
| Return True if S ends with the specified suffix,False otherwise.
#如果S以指定的后缀结尾,则返回True,否则返回False。
| With optional start, test S beginning at that position.With optional end, stop comparing S at that position.#选择切片范围
| suffix can also be a tuple of strings to try.
# 后缀可以是一个元组 元组如下
#tup1 = ('Google', 'Runoob', 1997, 2000)
#tup2 = (1, 2, 3, 4, 5, 6, 7 )
|
# endswith
str3 = '123abcd!!!'
print(str3.endswith('!'))# true
print(str3.endswith('!!'))#true
print(str3.endswith('!!!'))#true
print(str3.endswith(('!!!!!')))#false
print(str3.endswith('a'))#false
print(str3.endswith('a',0))#false
print(str3.endswith('a',0,1))#true
print(str3.endswith('c',1,3))#true end取不到
#print(str3.endswith((1)))
# 元组
strtup = '123abcd123!!!'
#print(strtup.endswith(((1,2,3)))) typeerror
print(strtup.endswith(('1','2','3'))) #true 必须 字符串
# 顺序不一样怎么办
print(strtup.endswith(('2','1','3','!'))) #true
print(strtup.endswith(('@','!')))#false
print(strtup.endswith(('!','1','2','!','3')))#true?
print(strtup.endswith(('!','1','2','!','3'),0,-4))#true
# 自助排列??
print('?')
print(strtup.endswith(('!','2','!'),0,-4))#true
字符串内置函数–endswith() /startswith() 带元组
- suffix参数如果是元组,那么必须是字符串类型的,我感觉和元祖的类型有关系,目前还没学,后面补
- 如果suffix参数是元组,元组内容的顺序没有关系,也就是说,只要在start,end范围之内,元组内只要包含了末尾的后缀,就返回false,如果元组的范围大于末尾也没有关系,只要
元组内容是字符串类型,并且在切片范围内,含有后根词缀即可返回true
,也就是遍历元祖中的内容,含有及true。 str.endswith('')
输出 都是true
# 元组
strtup = '123abcd123!!!'
#print(strtup.endswith(((1,2,3)))) typeerror
print(strtup.endswith(('1','2','3'))) #true 必须 字符串
# 顺序不一样怎么办
print(strtup.endswith(('2','1','3','!'))) #true
print(strtup.endswith(('@','!')))#false
print(strtup.endswith(('!','1','2','!','3')))#true?
print(strtup.endswith(('!','1','2','!','3'),0,-4))#true
# 自助排列??
print('?')
print(strtup.endswith(('!','2','!'),0,-4))#true
字符串内置函数–expandtabs() 制表符转换为空格
str.expandtabs(n)
将str 语句中的tab\t
转换成n个空格,类似文本缩进。\t
是 tab键,测试的时候要输入- 从第一个
\t
(起始)到下一个\t
之间的字符串+空格
等于8的整数倍 例:abcde\tabd
这里的\t会转换成3个空格,因为前面是5位。 ;又可以abc\tabd
这里\t就会补5个空格~ - 在tabsize<=8 的时候都是以补8个为准,大于8的时候才会按照数字增加。
- 但是在python3中,是补4的整数倍。
- help中显示
expandtabs(...)
| S.expandtabs(tabsize=8) -> str # 返回字符串
|
| Return a copy of S where all tab characters are expanded using spaces.
# 将字符串中的制表符号 \t 全部变成 控股
| If tabsize is not given, a tab size of 8 characters is assumed.
#如果tab键的大小没有给定,默认是8
# expandtabs
extabs = 'abcdefghijklmnopqrstuvwxyz'
print(extabs)
extabst ='\tabcde\tfghijklmnopqrstuvwxyz'
print(extabst)
print(extabst.expandtabs(4))
print(extabst.expandtabs(8)) # 默认是8
print(extabst.expandtabs(9))
print(extabst.exp