python常用函数和方法-Python字符串string常用方法和函数

join合并,以join前的string为分隔符,将列表中的元素合并为一个新的字符串

str_1="*".join(["Are","you","ok"])

print(str_1)

#结果Are*you*ok

分隔,split将string根据分隔符分隔成列表,也可以带参数num(分隔次数)

splitlines,按照行(" ", " ", ")分隔,可以带参数是否保留换行符(" ", " ", "),默认为 False,不包含换行符,如果为 True,则保留换行符

print(str.split())

#结果["hello", "world"]

print(str.split("o"))

#结果["hell", " w", "rld"]

print(str.split("o",1))

#结果["hell", " world"]

strline="hello word"

print(strline.splitlines(),",",strline.splitlines(True))

#结果["hello", "word"] , ["hello ", "word"]

字符串的开始与结束判断endwith,startswith

#3.1endwith判断以什么结尾,参数为1字符串,2开始位置,3结束位置

print(str.endswith("ld"),str.endswith("l"),str.endswith("o",2,5),str.endswith("l",2,5))

#结果True False True False

#3.2startswith判断以什么开始,同理

print(str.startswith("he"),str.startswith("e"),str.startswith("h",1,4),str.startswith("e",1,4))

#结果True False False True

字符串的isX判断

print(str.isalnum(),str.isalpha(),str.isascii(),str.isdecimal(),str.islower())

#结果False False True False True

isX说明,都是返回True或者Flase

string.isalnum() 如果 string 至少有一个字符并且所有字符都是字母或数字

string.isalpha()如果 string 至少有一个字符并且所有字符都是字母

string.isdecimal()如果 string 只包含十进制数字

string.isdigit()如果 string 只包含数字

string.islower()如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写

string.isnumeric()如果 string 中只包含数字字符

string.isspace()如果 string 中只包含空格

string.istitle()如果 string 是标题化的(见 title(),每个单词都是首字母大写)

string.isupper()如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写

字符串的大小写格式化,title标题化(每个单词首字母大写),upper全大写,lower全小写,capitalize首字母大写,swapcase翻转大小写

print(str.title(),"**",str.lower(),"**",str.upper(),"**",str.capitalize(),"**",str.swapcase())

#结果Hello World ** hello world ** HELLO WORLD ** Hello world ** HELLO WORLD

字符串的位置对齐格式化,center居中,rjust右对齐,ljust左对齐,参数1是对齐的位置,参数2对齐的字符,

特殊的对齐:zfill返回指定长度的字符串,原字符串右对齐,前面填充0

print(str.center(20),",",str.center(20,"*"))

#结果 hello world ,****hello world*****

print(str.rjust(20),",",str.rjust(20,"*"))

#结果 hello world , *********hello world

print(str.ljust(20),",",str.ljust(20,"*"))

#结果hello world , hello world*********

print(str.zfill(20))

#000000000hello world

print(str.ljust(20),",",str.ljust(20,"*"))

字符串的删除指定字符的格式化,strip去掉首尾的指定字符(默认空格),srtip是去掉右边的,lstrip是去掉左边的

str1= " hello world "

print(str1.strip(),",",str.strip("d"))

#结果hello world , hello worl

print(str1.rstrip(),",",str.rstrip("d"))

#结果 hello world , hello worl

print(str1.lstrip(),",",str.lstrip("h"))

#结果hello world , ello world

字符串的查找,find,查找是否包含某字符,可以带开始和结束位置,找到就返回索引值,没找到就返回-1

index与find作用一致,但没找到会报异常,也可以带开始和结束位置,rfind和rindex从右向左查询,其余参数一致

print(str.find("o"),str.find("a"),str.find("o",10,20))

#结果4 -1 -1

print(str.index("o"),end=",")

try:

print(str.index("a"))

except:

print("error")

#结果4,error

print(str.rfind("o"),str.rindex("o"))

#结果7 7

字符的替换,replace替换字符(默认全部替换,可以指定次数),expandtabs可以将tab替换成空格,值为空格的个数,默认8(等于tab的空格)

print(str.replace("o","a"),",",str.replace("o","a",1))

#结果hella warld , hella world

strtab="hello world"

print(strtab,",",strtab.expandtabs(1))

#结果hello world , hello world

partition() 方法用来根据指定的分隔符将字符串进行分割,像 find()和 split()的结合体,返回的元组,rpartition从右边开始

strwww="www.baidu.com"

print(strwww.partition("."),strwww.rpartition("."))

#结果("www", ".", "baidu.com") ("www.baidu", ".", "com")

format,格式化,类似%,format 函数可以接受不限个参数,位置可以不按顺序

print("{} {}".format("hello", "world") )

#结果hello world

print("{0} {1} {0}".format("hello", "world") )

#结果hello world hello

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值