字符串常用操作[补充]

字符串常用操作补充

1、capitalize

把字符串的第一个字符大写

mystr.capitalize()
>>> mystr = 'hello world and'
>>> mystr.capitalize()
'Hello world and'

2、title

把字符串的每个单词首字母大写

>>> a = "hello study"
>>> a.title( )
'Hello Study'

3、startswith

检查字符串是否是以hello开头,是则返回True,否则返回False

mystr.startswith(hello)
>>> mystr ='hello world '
>>> mystr.startswith("hello" )
True
>>> mystr.startswith( "Hello" )
False

4、endswith

检查字符串是否以obj结束,如果是返回True,否则返回False.

mystr.endswith(obj)
>>> mystr = 'hello world cpp'
>>> mystr.endswith('cpp' )
True
>>> mystr.endswith('app' )
False

5、 lower

转换mystr中所有大写字符为小写

mystr.lower( ) 
>>> mystr ='HELLO world ’
>>> mystr.Lower()
'hello world'

6、upper

转换mystr中的小写字母为大写

mystr.upper()
>>> mystr = 'HELLO worLd  and '
>>> mystr.upper()
'HELLO HORLD  AND '

7、ljust

返回一个原字符串左对齐,并使用空格填充至长度width的新字符串

mystr.ljust(width)
>>> mystr="hello"
>>> mystr.ljust(10)
'hello     '

8、rjust

返回一个原字符串右对齐,并使用空格填充至长度width的新字符串

mystr.rjust(width)
>>> mystr="hello"
>>> mystr.rjust(10)
'     hello'

9、center

返回一个原字符串居中,并使用空格填充至长度width的新字符串.

mystr.center(width)
>>> mystr ='heLLo world '
>>> mystr.center(20)
'    heLLo worLd    '

10、lstrip

删除mystr左边的空白字符

mystr.lstrip()
>>> mystr="     hello"
>>> mystr.lstrip( )
'hello'
>>> mystr="    hello     "
>>> mystr.lstrip()
'hello     ' 

11、rstrip

删除mystr字符串末尾的空白字符

mystr.rstrip()
>>> mystr="hello      "
>>> mystr.rstrip( )
'hello'
>>> mystr="    hello     "
>>> mystr.rstrip()
'     hello' 

12、strip

删除mystr字符串两端的空白字符

>>> a = "\n\asfg\t\n"
>>> a.strip()
'asfg'

13、rfind

类似于find()函数,不过是从右边开始查找.

mystr.rfind(str,start=0,end=len(mystr))
>>> mystr ='heLLo world'
>>> mystr.rfind('world')
6

14、rindex

类似于index(),不过是从右边开始.

mystr.rindex( str,start=0,end=len(mystr))
>>> mystr ='heLLo world'
>>> mystr.rindex('wo')
6

15、partition

把mystr以str分割成三部分,str前,str和str后

mystr.partition(str)
>>> mystr ='heLLo world ad gb ij dc'
>>> mystr.partition('ad')
('heLLo world ', 'ad', ' gb ij dc')

16、rpartition

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

mystr.rpartition(str)

17、splitlines

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

mystr.splitlines()
>>> mystr ='heLLo\nworld'
>>> mystr.splitlines()
['heLLo', 'world']

18、isalpha

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

mystr.isalpha()
>>>mystr='ac'
>>>mystr.isalpha()
True
>>>mystr='ac 12'
>>>mystr.isalpha()
False

19、isdigit

如果mystr只包含数字则返回True否则返回False.

mystr.isdigit()
>>>mystr='12'
>>>mystr.isdigit()
True
>>>mystr='ac 12'
>>>mystr.isdigit()
False

20、isalnum

如果mystr所有字符都是字母或数字则返回True,否则返回False

mystr.isalnum()
>>> mystr='acb'
>>> mystr.isalnum()
True
>>>mystr='ac 12'
>>>mystr.isalnum()
False
>>> mystr='ac1'
>>> mystr.isalnum()
True

21、isspace

如果mystr中只包含空格,则返回True,否则返回False.

mystr.isspace()
>>> mystr='avf123'
>>> mystr.isspace()
False
>>> mystr='  '
>>> mystr.isspace()
True

21、isspace

如果mystr中只包含空格,则返回True,否则返回False.

mystr.isspace()
>>> mystr='avf123'
>>> mystr.isspace()
False
>>> mystr='  '
>>> mystr.isspace()
True
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小橙子*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值