字符串你的内置功能
# separator 分隔符 substring子字符串
format
# 格式化输出 format(self , args , kwargs)
res = "my name is {} , my age is {}".format('sun',18) # 第一种方式 按照顺序
res1 = "my name is {0} , my age is {1}".format('sun',18) # 第二种方式 索引
res2= "my name is {name} , my age is {}".format(name='sun',age=18) #第三种方式 按照变量取值
encode
# 编码格式 encode(self,encoding,error )
find
# 查找关键字符串,并得到索引值 find(self, sub, __start,__endwith) 当关键字不存在时,显示-1,不会报错
topic = "hello world , i love you"
res = topic.find('i')
print(res)
14
index
# 查找关键字获得索引值 index(self, sub, __start,__endwith) 当关键字不存在时,会报错
topic = "hello world , i love you"
res = topic.index('i')
print(res)
14
isidentifier
# 判断是否可以作为变量名 isidentifier(self)
name = "1123sun"
print(name.isdentifier())
False # 变量名不可以由数字开头
format_map
# ?
expandtabs
# 设置tab键缩进字符个数 expandtabs(self, tabsize)
name = 'sun\tsun'
res = name.expandtabs(15)
print(res)
sun sun
casefold
# 返回无大小写的字符串 casefold(self)
name = 'SUn1111'
res = name.casefold()
print(res)
sun1111
capitalize
# 首字母大写 capitalize(self)
name = "sun"
res = name.capitalize()
print(res)
Sun
join
# 将迭代的对象加入你所需的字符 join(self,__iterable)
new = 'hello'
res = '*'.join(new)
print(res)
h*e*l*l*o
isdigit
# 判断是否是阿拉伯数字 isdigit(self)
num = "123"
print(num.isdigit())
True
isdecimal
# 判断数字 isdecimal(self)
isnumeric
# 判断是否是数字,包含中文数字'四' isnumeric(self)
split
# 指定分隔符,切割,切割次数 split(self,sep,maxsplit)
topic = "hello world i love you"
res = topic.split()
print(res)
["hello","world","i","love","you"]
strip
# 去除字符串俩边的特殊符号,默认是空格 strip(self,__chars)
name = " sun "
res = name.strip()
print(res)
sun
lstrip
# 去除字符串左边的特殊符号,默认是空格 lstrip(self,__chars)
rstrip
# 去除字符串右边的特殊符号,默认是空格 rstrip(self,__chars)
count
# 统计指定字符串出现的次数,可以按索引范围寻找 count(self,sub,_start,_end)
name = "sun"
res = name.count('u')
print(res)
1
upper
# 将字符串转换为大写 upper(self)
lower
# 将字符串转换为小写 lower(self)
replace
# 替换字符串,可以修改次数 count(self,_old,_new,count)
endswith
# 判断以什么字符串结尾 endwith(self,suffix,start,end)
startswith
# 判断以什么字符串开始 startswith(self,prefix,start,end)
center
# 指定字符串俩边的占位数和符号 center(self,__width,_fillchar)
name = 'sun'
res = name.center(20,'*')
print(res)
********sun*********
isalnum
# 判断字符串是否是由字符或数字组成 isalnum(self)
name = "123sun"
print(name.isalnum())
True
isalpha
# 判断字符串是否是由纯字母组成 isalpha(self)
name = "sun"
name.isalpha()
print(name.isalpha())
True
isascii
# 判断字符串是否在ASCII表中 isascii(slif)
islower
# 判断字符串是否都是小写 islower(self)
isupper
# 判断字符串是否都是大写 isupper()
isprintable
# 判断这个字符是否可以打印 isprintable(self)
isspace
# 判断这个字符串是否为空 isspace(self)
istitle
# 判断这个字符串是否是标题(每个单词首字母大写) istitle()
ljust
# 字符串靠左对齐开始,指定占位宽度和指定符号 ljust(self,__width,__fillchar)
partition
# 指定字符串隔符,分成一个元组,三个元素 partition(self,__sep)
name = 'sun'
res = name.partition('u')
print(res)
('s', 'u', 'n')
rpartition
# 从右边指定字符串隔符,分成三个元组 rpartition(self,__sep)
name = 'sunununun'
res = name.rpartition('u')
print(res)
('sununun', 'u', 'n')
removeprefix
# ?
removesuufix
# ?
rfind
# 从右边找子字符串,并获得索引值 rfind(self,sub,__start,__end)
rindex
# 从右边找子字符串,并获得索引值 rindex(self,sub,__start,__end)
rjust
# 字符串靠右对齐开始,指定占位宽度和指定符号 rjust(self,__width,__fillchar)
rsplit
# 从右边指定分隔符,分割次数 rspilt(self,sep,maxsplit)
rstrip
# 删除字符串右边指定的符号 rstrip(self,__chars)
splitlines
# ?
swapcase
# 将大写字符转换为小写字符,将小写字符转换为大写字符 swapcase(self)
title
# 字符串的首字母大写 title(self)
translate
# ?
zfill
# 指定字符从占位符位‘0’,默认右对齐 zfill(self,__width)
name = 'sun'
res = name.zfill(20)
print(res)
00000000000000000sun
该博客主要介绍了Python中字符串的内置功能,涵盖了分隔符、子字符串相关操作,以及如format、encode、find等众多方法,涉及字符串的格式化、查找、判断、大小写转换、分割、去除前后缀等多方面功能。

2835





