python中字符串常见操作

<1>find
检测 mystr 是否包含在 str中,如果是返回开始的索引值,否则返回-1

str = 'hello zhangsan,i love you'
mystr = 'zhangsan'
index=str.find(mystr)
print(index)
==输出结果==
6

<2>index
跟find()方法一样,只不过如果mystr不在 str中会报一个异常.
<3>count
返回 str在start和end之间 在 mystr里面出现的次数
str = ‘hello zhangsan hello lisi’
mystr = ‘hello’
count=str.count(mystr)
print(count)
==运行结果==
2
<4>replace
把 str 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.

str = 'hello zhangsan hello lisi'
str1 = 'hello'
str2='你好'
str=str.replace(str1,str2,1)
print(str)
==运行结果==
你好 zhangsan hello lisi

<5>split
以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 次字符串

str = 'hello zhangsan hello lisi'
strs= str.split(sep=' ',maxsplit=2)
for s in strs:
print(s)
==运行结果==
hello
zhangsan
hello lisi

<6>capitalize
把字符串的第一个字符大写

str = 'hello world'
str=str.capitalize()
print(str)
==运行结果==
Hello world

<7>title
把字符串的每个单词首字母大写

str = 'hello world'
str=str.title()
print(str)
==运行结果==
Hello World

<8>startswith
检查字符串是否是以 title 开头, 是则返回 True,否则返回 False

str = 'hello world'
title="hello"
tag=str.startswith(title)
print(tag)
==运行结果==
True

<9>endswith
检查字符串是否以title 结束,如果是返回True,否则返回 False.

str = 'hello world'
title="world'"
tag=str.endwith(title)
print(tag)
==运行结果==
True

<10>lower
转换 str 中所有大写字符为小写

str=’HELLO’
str=str.lower()      
print(str)  
==运行结果==
hello

<11>upper
转换 str 中的小写字母为大写

str=’hello’
str=str.upper()      
print(str)  
==运行结果==
HELLO

<12>ljust
返回一个原字符串左对齐,并使用mystr填充至长度 width 的新字符串

str='hello'
mystr='+'`这里写代码片`
width=10
str=str.ljust(width,mystr)
print(str)
==运行结果== 
hello+++++

<13>rjust
返回一个原字符串右对齐,并使用mystr填充至长度 width 的新字符串

str='hello'
mystr='+'
width=10
str=str.rjust(width,mystr)
print(str)
==运行结果== 
+++++hello

<14>center
返回一个原字符串居中,并使用mystr填充至长度 width 的新字符串

str='hello'
mystr='+'`这里写代码片`
width=10
str=str.rjust(width,mystr)
print(str)
==运行结果== `这里写代码片`
++hello+++   

<15>lstrip
删除 str 左边的空白字符
str.lstrip()
<16>rstrip
删除 str 字符串末尾的空白字符
str.rstrip()
<17>strip
删除str字符串两端的空白字符
str.strip()
<18>partition
把str以mystr分割成三部分,mystr前,mystr和mystr后

str='hello beijing 你好'
mystr='beijing'
strs= str.partition(mystr)
for s in strs:
print(s)
==运行结果==
hello 
beijing
 你好

<19>rpartition
类似于 partition()函数,不过是从右边开始.

str='hello beijing 你好 beijing world'
mystr='beijing'
strs= str.rpartition(mystr)
for s in strs:
print(s)
==运行结果==
hello beijing 你好 
beijing
 world

<20>splitlines
按照行分隔,返回一个包含各行作为元素的列表

str='hello beijing\nhello beijing\nhello shanghai'
strs= str.splitlines()
for s in strs:
print(s)
==运行结果==
hello beijing
hello beijing
hello shanghai

<21>isalpha
如果 mystr 所有字符都是字母 则返回 True,否则返回 False

mystr='hellobeijing'
ischar=mystr.isalpha()
print(ischar)
==运行结果==
True
<22>isdigit
如果 mystr 只包含数字则返回 True 否则返回 False.
mystr='123456'
isnum=mystr.isdigit()
print(isnum)
==运行结果==
True

<23>isalnum
如果 mystr 所有字符都是字母或数字则返回 True,否则返回 False
mystr.isalnum()
<24>isspace
如果 mystr 中只包含空格,则返回 True,否则返回 False.
mystr.isspace()
<25>join
mystr 中每个字符后面插入str,构造出一个新的字符串

str='hello'
mystr='你好啊'
newstr=str.join(mystr)
print(newstr)
==运行结果==
你hello好hello啊
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值