python数据类型之str⽤法
1、⾸字母⼤写
语法:S.capitalize() -> str
title = "today is a good day"
title_ca = title.capitalize()
print(title_ca)
结果:today is a good day
2、⼤写转⼩写
1语法:S.casefold() -> str
2
3 title = "TODAY is a GOOD day"
4 title_ca = title.casefold()
5 print(title_ca)
结果:Today is a good day
3、字符串居中
c = 'kong'
ret = c.center(10,'*')
print(ret)
结果:***kong***
4、字符串⼦串数量统计
title = "today is a good day"
title_ca = unt('y',1,5)
print(title_ca)
结果:1
5、中⽂转UTF-8
zw = '孔扎根'
ut = zw.encode()
print(ut)
结果:b'\xe7\xa9\xba\xe6\x89\x8e\xe6\xa0\xb9'
6、字符串结束判断
title = "TODAY is a GOOD day"
title_ca = dswith('day')
print(title_ca)
结果:True
7、TAB转空格
#默认是⼀个TAB转8个空格
title = "TODAY\tis\ta\tGOOD\tday"
title_ca = pandtabs()
print(title_ca)
结果:TODAY  is      a      GOOD    day
8、查字符串的位置
S.find(sub[, start[, end]]) -> int
title = "TODAY\tis\ta\tGOOD\tday"
title_ca = title.find('s')
print(title_ca)
9、字符串格式化
S.format(*args, **kwargs) -> str
#可传⼊元组或字典
title = "{}\tis\ta\t{day_type}\tday"
title_ca = title.format('TODAY',day_type='GOOD')
print(title_ca)
结果:TODAY    is    a    GOOD    day
10、字符串格式化,从字典输⼊(format_map)
S.format_map(mapping) -> str
#输⼊参数为字典,循环读字典中的列表
maping_name = {
'name':['alex','join'],
'age':[18,19]
}
for x in range(2):
print('my name is {},and i is {} old'.format(maping_name['name'][x],maping_name['age'][x]))结果:
my name is alex,and i is18 old
my name is join,and i is19 old
11、字符串的索引位置
S.index(sub[, start[, end]]) -> int
#查Y的索引位置,从0开始数
title = "TODAY\tis\ta\tGOOD\tday"
title_ca = title.index('Y')
print(title_ca)
结果:4
12、字符串中⾄少有⼀个数字
S.isalnum() -> bool
#字符串不能有空格,否则失败
title = "22TODAY"
title_ca = title.isalnum()
print(title_ca)
结果:True
13、字符串中⾄少有⼀个字母
S.isalpha() -> bool
#字符串不能有空格或TAB键
title = "22TODAY"
title_ca = title.isalnum()
print(title_ca)
结果:True
14、字符串是否为数值
num = "1" #unioncode
print(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())
结果:
True
True
True
num = "1"#全⾓
print(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())
结果:
True
True
True
num = b"1"#byte
print(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())
结果:
AttributeError: 'bytes'object has no attribute 'isdecimal' AttributeError: 'bytes'object has no attribute 'isnumeric'
num = "IV"#罗马字符
print(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())
结果:
False
False
False
num = "四"#汉字
print(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())
结果:
False
False
True
⽀持的字符:
isdigit:⽀持        unioncode,全⾓,byte,汉字
isdecimal:⽀持  unioncode,全⾓,
isnumeric:⽀持  unioncode,全⾓,汉字
报错:
isdigit不会报错,后两种在byte判断时会报错
15、判断字符串是否为有效标识符(可做为函数名称)#S.isidentifier() -> bool
t_code = 'test'
print(t_code.isidentifier())
结果:返回True
t_code = '23test'
print(t_code.isidentifier())
结果:返回False
16、判断字符串是否全⼩写
#S.islower() -> bool
t_code = 'kongzhagen'
print(t_code.islower())
结果:返回True
t_code = 'kongzHagen'
print(t_code.islower())
结果:返回False
17、判断字符串是否全整型数字
#S.isnumeric() -> bool
t_code = '123'
print(t_code.isnumeric())
结果:返回True
t_code = '123d'
print(t_code.isnumeric())
结果:返回False
t_code = '123.123'
print(t_code.isnumeric())
结果:返回False
18、判断所有字符是否可打印
#S.isprintable() -> bool
t_code = '123KKK'
print(t_code.isprintable())
返回:True
t_code = ''
print(t_code.isprintable())
返回:True
t_code = 'KKK\n\t'
print(t_code.isprintable())
返回:False
19、判断字符中是否全为空格
#S.isspace() -> bool
t_code = ''
print(t_code.isspace())
结果:返回True
t_code = '123 KKK'
print(t_code.isspace())
结果:返回False
20、判断字符串是否为标题格式(⾸字母⼤写)
#S.istitle() -> bool
t_code = 'Today Is A Good Day'
print(t_code.istitle())
结果:True
t_code = 'Today Is A Good day'
print(t_code.istitle())
结果:False
t_code = 'TODAY IS'
print(t_code.istitle())
结果:False
21、判断字符串是否全⼤写
#S.isupper() -> bool
t_code = 'Today Is A Good day'
print(t_code.isupper())
结果:False
t_code = 'TODAY IS'
print(t_code.isupper())
结果:True
22、字符串连接
#S.join(iterable) -> str
t_code = 'Today Is A Good Day'
t1_code = '.'.join(t_code)
print(t1_code)
结果:T.o. .I.s. .A. .d. .D.a.y
23、左对齐,达不到指定长度,右则填充
#S.ljust(width[, fillchar]) -> str
t_code = 'Today Is A Good Day'
print(t_code.ljust(22,'*'))
结果:Today Is A Good Day***
24、转⼩写
#S.lower() -> str
t_code = 'Today Is A Good Day'
print(t_code.lower())
结果:today is a good day
25、左边去除指定字符,默认为空格
# S.lstrip([chars]) -> str
t_code = 'Today Is A Good Day'
print(t_code.lstrip('T'))
结果:oday Is A Good Day
t_code = '  Today Is A Good Day'
print(t_code.lstrip())
结果:Today Is A Good Day
26、
maketrans
27、partition
按指定的字符拆分字符串,分头、分隔串、尾,未到指定的分隔符,头返回⾃⼰,后⾯两个返回空#S.partition(sep) -> (head, sep, tail)
t_code = 'TodayIs A Good Day'
print(t_code.partition('a'))
结果:('Tod', 'a', 'yIs A Good Day')
print(t_code.partition('P'))
结果:('TodayIs A Good Day', '', '')
28、replace:字符串替换
将⽼字符串替换为新字符串,可指定替换次数
#S.replace(old, new[, count]) -> str
t_code = 'TodayIs A Good Day,To today'
print(place('T','M',2))
结果:ModayIs A Good Day,Mo today
29、rfind:返回查询到的字符串的最⼤索引
#S.rfind(sub[, start[, end]]) -> int
t_code = 'TodayIs A Good Day,To today'
print(t_code.rfind('d'))
结果:24
30、rindex:类似rfind,但如果没到会报错
#S.rindex(sub[, start[, end]]) -> int
t_code = 'TodayIs A Good Day,To today'
print(t_code.rindex('p'))
结果:
Traceback (most recent call last):
File "C:/51py/day1/study.py", line 90, in <module>
print(t_code.rindex('p'))
ValueError: substring not found
31、rjust:右对齐,左侧填充字符
#S.rjust(width[, fillchar]) -> str
t_code = 'Today'
print(t_code.rjust(10,'*'))
结果:*****Today
32、rpartition:类似partition,如果未到字符串,则空值在左边
#S.rpartition(sep) -> (head, sep, tail)
字符串长度判断t_code = 'Today is a good day'
print(t_code.rpartition('isa'))
结果:('', '', 'Today is a good day')
33、rsplit:分割字符串,从右边开始
#S.rsplit(sep=None, maxsplit=-1) -> list of strings
t_code = 'Today is a good day'
print(t_code.rsplit('o',1))
结果:['Today is a go', 'd day']
34、rstrip:右边去空格
#S.rstrip([chars]) -> str
t_code = '  Today is a good day  '
print(t_code.rstrip())
结果:  Today is a good day
35、splitlines:⽅法返回⼀个字符串的所有⾏列表,可选包括换⾏符的列表(如果num提供,则为true) #S.splitlines([keepends]) -> list of strings
t_code = 'Today\n is\n a\n good\n day'
print(t_code.splitlines())
print(t_code.splitlines(0))
print(t_code.splitlines(1))
结果:
['Today', ' is', ' a', ' good', ' day']
['Today', ' is', ' a', ' good', ' day']
['Today\n', ' is\n', ' a\n', ' good\n', ' day']
36、startswith:如果字符串以指定的字符为前缀,则返回true,否则返回false
#S.startswith(prefix[, start[, end]]) -> bool
t_code = 'Today\n is\n a\n good\n day'
print(t_code.startswith('Today'))
结果:True
37、strip:去除字符串前后的空格
#S.strip([chars]) -> str
t_code = ' Today\n is\n a\n good\n day '
print(t_code.strip())
结果:
Today
is
a
good
day
38、swapcase:⼤⼩写互转

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。