当前位置: 移动技术网 > IT编程>脚本编程>Python > python-字符串的常见操作

python-字符串的常见操作

2020年07月16日  | 移动技术网IT编程  | 我要评论

join: 将序列中的元素以指定的字符连接生成一个新的字符串。

flag_str="_"
my_str="abc"
result=flag_str.join(my_str)
print(result)

在这里插入图片描述

'-'.join(('a', 'b', 'c'))

在这里插入图片描述

strip,rstrip, lstrip

用于移除字符串左右两边、左边、右边指定的字符(默认为空白符,例如:/n, /r, /t, ’ ')或字符序列。
语法

str.strip([chars])
str.lstrip([chars])
str.rstrip([chars])
my_str=" hello "
my_str.strip()
my_str.rstrip()

在这里插入图片描述

my_str=" hello "
my_str.rstrip()

在这里插入图片描述

my_str=" hello "
my_str.lstrip()

在这里插入图片描述
2)移除一个字符串列表(是否会删除的前提是从字符串最开头和最结尾是不是包含要删除的字符,如果有就会继续处理,没有的话是不会删除中间的字符的):

 'abc@163.com'.strip('cawm')
'bc@163.co'

'abc@163.com'.lstrip('cawm')
'bc@163.com'

 'abc@163.com'.rstrip('cawm')
'abc@163.co'

my_str="aaaaa"
result=my_str.strip("a")
print(result)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

lower upper title capitalize swapcase

lower:将字符串中的大写字母转为小写字母。

upper:将字符串中的小写字母转为大写字母。

title:将所有单词首字母转为大写,其余字母均转为小写。

capitalize:将字符串的第一个字母转为大写,其他字母转为小写。

swapcase:将字符串做大小写字母转换(大写->小写,小写->大写)
语法

str.lower()
str.upper()str.title()str.capitalize()str.swapcase()
'aBcDe'.upper()
'ABCDE'

'aBcDe'.lower()
'abcde'

'thIs is a exaMple'.title()
'This Is A Example'

'this is A example'.capitalize()
'This is a example'

'aBcDe'.swapcase()
'AbCdE'

find index rfind rindex

find:检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。

index:检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。

rfind:类似于find()函数,只不过是从字符串右边开始查找。

rindex:类似于index()函数,只不过是从字符串右边开始查找。
语法

str.find(str, beg=0, end=len(string))
str.index(str, beg=0, end=len(string))

str – 指定检索的字符串
beg – 开始索引,默认为0。
end – 结束索引,默认为字符串的长度。

str1 = 'This is a example!'
str2 = 'example'
index = str1.find(str2, 0, len(str1))
print(index)
10

str1 = 'This is a example!'
str2 = 'example'
index = str1.index(str2, 0, len(str1))
print(index)
10

count

统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

str.count(sub, start= 0, end=len(string))
'hello world'.count('o')
2
'hello world'.count('o', 5 , len('hello world'))
1

split:指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串。

splitlines:按照行(’\r’, ‘\r\n’, \n’)分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。

partition:根据指定的分隔符将字符串进行分割。如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。

rpartition:类似于partition()函数,只不过是从右边开始。
语法

str.split(str="", num=string.count(str))

str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
num – 分割次数。

str.splitlines([keepends])

keepends – 在输出结果里是否保留换行符(’\r’, ‘\r\n’, \n’),默认为 False,不包含换行符,如果为 True,则保留换行符。

str.partition(str)

str-- 指定的分隔符。

str.rpartition(str)
>>> 'This \nis a \nexample'.split()
['This', 'is', 'a', 'example']

>>> 'This \nis a \nexample'.split(' ', 1)
['This', '\nis a \nexample'

>>> 'This \nis a \nexample'.splitlines()
['This ', 'is a ', 'example']

>>> 'This \nis a \nexample'.splitlines(True)
['This \n', 'is a \n', 'example']

>>> 'www.example.com'.partition('.')
('www', '.', 'example.com')

>>> 'www.example.com'.rpartition('.')
('www.example', '.', 'com')

本文地址:https://blog.csdn.net/weixin_43506858/article/details/107346980

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网