1.partition方法
a=‘123ab456’
x=a.partition(‘ab’)
print(x)
partition方法把字符串以一个子串前后分割,包括自己也成为了其中一个子串 注意:如果后面还有分割子串 则不管它了
2.split方法
a=‘123a456a789’
b=a.split(‘a’)
print(b)
split也是子串隔断字符串的方法,不同的是,子串本身隔断后没有了
3.splitlines
a=‘abc\n123\n45’
b=a.splitlines()
print(b)
此方法即按照换行符进行分割
4.replace
a=‘123abc456abc’
b=a.replace(‘abc’,‘123’)
print(b)
此方法为替换指定字符串
5.maketrans
s=‘123456789111’
a=‘1234567’
b=‘abcdefg’
table=str.maketrans(a,b)
b=s.translate(table)
print(b)
此方法为建立映射表,a/b长度必须一样,组成映射表,通过映射表,进行替换生成新的表
6.center
a=‘123’
b=a.center(7,’*’)
print(b)
7.rjust ljust
a=‘123’
b=a.rjust(10,’
′
)
c
=
a
.
l
j
u
s
t
(
10
,
′
') c=a.ljust(10,'
′)c=a.ljust(10,′’)
print(b)
print©
此方法为填充符号
8.lstrip rstrip
a=’ 123 ’
b=a.lstrip()
print(b)
c=a.rstrip()
print©
此方法为去除空格
(待续未完)
Python中关于字符串的一些方法
最新推荐文章于 2023-05-27 18:19:14 发布