6、Python基础---字符串的常用操作方法(基于Python3.6)

1.find string = "Beautiful is better than ugly " 检查字符串是否包含在string中,如果包含则返回字符串开始的下标(索引),如果不包含则返回-1

>>> string='Beautiful is better than ugly '
>>> string.find('better')
13
>>> string.find('python')
-1


rfind 和find类似,不过是从右侧开始查找,因为有的字符串在原字符串中会有多个,但返回值只有第一个,所有rfind是属于右侧优先原则

2.index 作用和find一样,但如果不包含字符串则抛异常(报错),rindex和rfind类似

>>> string='Beautiful is better than ugly '
>>> string.index('better')
13
>>> string.index('python')
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    string.index('python')
ValueError: substring not found


3.count 返回字符串在目标字符串中出现的次数

>>> string='Beautiful is better than ugly '
>>> string.count('a')
2
>>> string.count('better')
1
>>> string.count(' ')
5
>>> string.count('python')
0


4.replace 将字符串中指定的字符串用其它字符串进行替换

>>> string='Beautiful is better than ugly '
>>> string.replace('Beautiful','beautiful')
'beautiful is better than ugly '


 
# 本行代码中的2指得是最多使用—_替换两个空格,第三个参数不写默认为0

>>> string.replace(' ','_',2)
'Beautiful_is_better than ugly '


5.split 以指定字符分割切片字符串,返回值为list列表,无参数默认为空格分割

>>> string='Beautiful is better than ugly '
# 不写参数默认使用空格分割,且连续多个空格视为一个
# 若空格在首部或尾部则不再往两端分割
>>> string.split()
['Beautiful', 'is', 'better', 'than', 'ugly']
>>> string.split(' ')
['Beautiful', 'is', 'better', 'than', 'ugly', '']
>>> string.split('e')
['B', 'autiful is b', 'tt', 'r than ugly ']


6.capitalize 将字符串首字母转换成大写

>>> string='beautiful is better than ugly '
>>> string.capitalize()
'Beautiful is better than ugly '


7.title 将字符串中每个单词的首写

>>> string='beautiful is better than ugly '
>>> string.title()
'Beautiful Is Better Than Ugly '


8.starswith 检查字符串开头是否包含指定字符串

>>> string='Beautiful is better than ugly '
>>> string.startswith('B')
True
>>> string.startswith('b')
False


9.endswith 检查字符串结尾是否包含指定字符串,和startswith类似

>>> string='Beautiful is better than ugly '
>>> string.endswith(' ')
True
>>> string.endswith('y')
False


10.lower 将字符串中所有的大写字母转化为小写

>>> string='Beautiful Is Better Than Ugly '
>>> string.lower()
'beautiful is better than ugly '


11.upper 将字符串中所有的小写字母转化为大写,和lower相反的作用

>>> string='beautiful is better than ugly '
>>> string.upper()
'BEAUTIFUL IS BETTER THAN UGLY '


12.ljust 返回一个原字符串左对齐,并使用空格来或指定字符填充剩余位置的字符串(默认为空格)

>>> string=' python'
>>> string.ljust(10)
' python   '
 
>>> string.ljust(10,'a')
' pythonaaa'


13.rjust 返回一个原字符串右对齐,并使用空格或指定字符来填充剩余位置的字符串(默认为空格)

>>> string=' python'
>>> string.rjust(10)
'    python'
 
>>> string.rjust(10,'a')
'aaa python'


14.center 返回一个原字符串居中对齐,并使用空格或指定字符来填充剩余位置的字符串(默认为空格)

>>> string=' python'
>>> string.center(10)
'  python  '
 
>>> string.center(10,'a')
'a pythonaa'


15.lstrip 删除目标字符串左侧的空格

     rstrip 删除目标字符串右侧的空格

     strip 删除目标字符串两侧的空格

>>> string='   python   '
>>> string.lstrip()
'python   '
 
>>> string.rstrip()
'   python'
 
>>> string.strip()
'python'


16.partition 以指定字符串分割原字符串为三部分,返回一个由这三部分组成的元组

    rpartiotion是从右侧开始查找,直到找到第一个符合的字符串

>>> string='Beautiful is better than ugly '
>>> string.partition('e')
('B', 'e', 'autiful is better than ugly ')
 
>>> string.rpartition('e')
('Beautiful is bett', 'e', 'r than ugly ')


17.splitlines 将原始字符串按照换行符分割,并返回分割后字符串的列表,和split("\n")效果一样

>>> string='Beautiful is better than \nugly '
>>> string.splitlines()
['Beautiful is better than ', 'ugly ']


18.isalpha 判断目标字符串是否全是字母,如果是则返回True,否则返回False

>>> string='Beautiful is better than ugly '
>>> string.isalpha()
False
 
>>> string='Beautiful'
>>> string.isalpha()
True


19.isalnum() 判断目标字符串是否由字母和数字组成。所有字符都是字母或数字则返回 True,否则返回 False

>>> string='this2019'
>>> string.isalnum()
True
>>> string='now is 2019'
>>> string.isalnum()
False


20.isspace 如果字符春中只包含空格则返回True,否则返回False

>>> string='   '
>>> string.isspace()
True
>>> string='a   '
>>> string.isspace()
False


21.join 将字符串或者列表、元组中每个元素使用指定字符串拼接成一个新字符串

>>> string='python'
>>> 'OK'.join(string)
'pOKyOKtOKhOKoOKn'
 
>>> list1=['123','abc','你好']
>>> '__'.join(list1)
'123__abc__你好'


 
 

发布了48 篇原创文章 · 获赞 49 · 访问量 1863

猜你喜欢

转载自blog.csdn.net/bjniujw1024/article/details/104385723