Python 基础(7-4) -- 字符串常见操作

目录

1 find

2 index

3 count

4 replace

5 split

6 capitalize

7 title

8 startswith   

9 lower、upper

10 ljust、rjust、center

11 lstrip、strip、strip

12 rfind

13 rindex

14 partition

15 rpartition

16 splitlines

17 isalpha

18 isdigit

19 isalnum

20 isspace

21 join


1 find

str.find(xxx, start=0, end=len(str))

检测xxx是否包含在str中,如果是返回开始的索引,否返回-1

>>> str = "you are a beautiful girl"
>>> str.find('are',0,len(str))
4

2 index

find()方法一,只不如果str不在 str中会一个异常.

str.index(xxx, start=0, end=len(str))

>>> str = "you are a beautiful girl"
>>> str.index('are',0,len(str))
4

3 count

返回 xxxstartend之间 str里面出现的次数

str.count(xxx, start=0, end=len(str))

>>> str.count("a",0,len(str))
3

4 replace

str 中的 xxx1 替换成 xxx2,如果 count 指定,则替换不超过 count .

str.replace(xxx1, xxx2,  str.count(xxx1))

>>> str.replace('a','A',2)
'you Are A beautiful girl'

5 split

xxx 为分隔符切片 str,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串

str.split(xxx=" ", 2)

>>> str.split(" ")
['you', 'are', 'a', 'beautiful', 'girl']
>>> str.split(" ", 3)
['you', 'are', 'a', 'beautiful girl']

6 capitalize

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

str.capitalize()

>>> str.capitalize()
'You are a beautiful girl'

7 title

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

>>> str.title()
'You Are A Beautiful Girl'

8 startswith   

检查字符串是否是以 xxx 开头, 是则返回 True,否则返回 False , str.startswith("xxx")

   endswith    检查字符串是否以xxx结束,如果是返回True,否则返回 False, str.endswith("xxx")

>>> str.startswith('yo')
True
>>> str.startswith('aa')
False
>>> str.endswith('girl')
True

9 lower、upper

        lower 转换 str 中所有大写字符为小写 str.lower()  

        upper 转换 str 中的小写字母为大写  str.upper()

>>> aa="HaHaHa"
>>> aa.lower()
'hahaha'
>>> aa.upper()
'HAHAHA'

10 ljust、rjust、center

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

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

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

>>> str.ljust(30)
'you are a beautiful girl      '
>>> str.rjust(30)
'      you are a beautiful girl'
>>> str.center(30)
'   you are a beautiful girl   '

11 lstrip、strip、strip

        lstrip   删除 str 左边的空白字符 str.lstrip()

        rstrip   删除 str 字符串末尾的空白字符 str.rstrip()

        strip    删除str字符串两端的空白字符 str.strip()

>>> aa = "    you are perfect    "
>>> aa.lstrip()
'you are perfect    '
>>> aa.rstrip()
'    you are perfect'
>>> aa.strip()
'you are perfect'

12 rfind

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

str.rfind(xxx, start=0,end=len(str) )

>>> cc="you are perfect and are you"
>>> cc.rfind('are',0,len(cc))
20
>>> cc.find('are',0,len(cc))
4

13 rindex

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

str.rindex( xxx, start=0,end=len(str))

cc.index("aa",0,len(cc))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> cc.index("are",0,len(cc))
4
>>> cc.rindex("are",0,len(cc))
20

14 partition

strxxx分割成三部分,xxx前,xxxxxx

str.partition(xxx)

>>> cc.partition('perfect')
('you are ', 'perfect', ' and are you')

15 rpartition

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

str.rpartition(xxx)

>>> Q="a boy like to the girl and girlrl"
>>> Q.partition('girl')
('a boy like to the ', 'girl', ' and girlrl')
>>> Q.rpartition('girl')
('a boy like to the girl and ', 'girl', 'rl')

16 splitlines

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

str.splitlines()

>>> dd="you are perfect\nand\nare you"
>>> print(dd)
you are perfect
and
are you
>>> dd.splitlines()
['you are perfect', 'and', 'are you']

17 isalpha

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

str.isalpha()

>>> A="hello"
>>> B="123heLL0123"
>>> A.isalpha()
True
>>> B.isalpha()
False

18 isdigit

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

str.isdigit()

>>> B="123heLL0123"
>>> C='1234'
>>> C.isdigit()
True
>>> B.isdigit()
False

19 isalnum

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

str.isalnum()

>>> A="hello"
>>> B="123heLL0123"
>>> C='1234'
>>> D="123gujh-*"
>>> A.isalnum()
True
>>> B.isalnum()
True
>>> C.isalnum()
True
>>> D.isalnum()
False

20 isspace

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

str.isspace()

>>> D="123gujh-*"
>>> E="    "
>>> E.isspace()
True
>>> D.isspace()
False

21 join

str 中每个元素前面插入xxx,构造出一个新的字符串

str.join(xxx)

>>> F="Love"
>>> G=['1','2','3']
>>> H="_"

>>> F.join(G)
'1Love2Love3'

>>> H.join(F)
'L_o_v_e'

>>> F.join(H)
'_'

>>> G.join(F)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'join'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值