对象方法的使用方式:对象.方法(参数)
一、查找计算:
len 计算字符串的字符个数(一个汉字或一个字母都算一个字符)
name = '我是aa\n'
num = len(name)
print(num)
输出:5
find 查找子串的索引/下标位置,从0开始找对应索引(找不到时返回-1)
msg = 'The day is a sunny day'
result1 = msg.find('d')
print(result1)
result2 = msg.find('d',5,-1)
print(result2)
result3 = msg.find('d',5,9)
print(result3)
输出:
4
19
-1
rfind 功能使用同find,区别是 从右边开始找对应索引
msg = 'The day is a sunny day'
result1 = msg.rfind('d')
result2 = msg.rfind('da')
print(result1)
print(result2)
输出:
19
19
index/rindex 返回要找的值的索引,与find/rfind唯一区别是:find/rfind找不到时返回-1,index/rindex找不到时直接报错
msg = 'The day is a sunny day'
result = msg.index('dd')
print(result)
输出:
Traceback (most recent call last):
File "xxx.py", line 43, in <module>
result = msg.index('dd')
ValueError: substring not found
count 计算某个子字符串出现的次数
msg = 'The day is a sunny day'
result1 = msg.count('a')
print(result1)
result2 = msg.count('a',6)
print(result2)
result3 = msg.count('a',6,12)
print(result3)
输出:
3
2
1
二、转换:
replace 使用给定的新字符串,替换原字符串中的旧字符/字符串
msg = 'The day is a sunny day'
result1 = msg.replace('day','DAY')
print(result1)
result2 = msg.replace('a','|',2)
print(result2)
result3 = msg.replace('a','A',1)
print(result3)
输出:
The DAY is a sunny DAY
The d|y is | sunny day
The dAy is a sunny day
capitalize 将字符串的首字母改成大写(只是将整个字符串的首字母大写,而不是将字符串中每个子字符串首字母大写)
s = 'jack tom'
s_new = s.capitalize()
print(s_new)
输出:
Jack tom
title 将字符串中每个单词的首字母大写
s = 'good day'
s1 = 'good day-sz*sz2--qq%yy'
s_new = s.title()
s1_new = s1.title()
print(s_new)
print(s1_new)
输出:
Good Day
Good Day-Sz*Sz2--Qq%Yy
lower 将字符串中的每个字符全变小写
s = 'Good Day HA'
s_new = s.lower()
print(s_new)
输出:
good day ha
upper 将字符串中的每个字符全变大写
s = 'good_day'
s_new = s.upper()
print(s_new)
输出:
GOOD_DAY
三、填充压缩:
ljust 、rjust 字符串从左边/右边开始,不够在后面/前面填充
s = 'girl'
s_new = s.ljust(30,'-')
s_new1 = s.rjust(30,'-')
print(s_new)
print(s_new1)
输出:
girl--------------------------
--------------------------girl
center 字符串按照指定长度两边填充
s = 'girl'
s_new = s.center(50,'-')
print(s_new)
输出:
-----------------------girl-----------------------
ltrip 左边空格、\n、tab键等都去掉
s = ' \ngood day \n \thello'
s_new = s.lstrip()
print(s_new)
输出:
good day
hello
rtrip 右边空格、\n、tab键等都去掉
s = ' good day \n \t'
s_new = s.rstrip()
print(s_new)
输出:
good day
四、分割拼接:
split 将一个大的字符串分割成几个子字符串
msg = 'The day is a sunny day'
result1 = msg.split()
print(result1)
result2 = msg.split('y')
print(result2)
result3 = msg.split(maxsplit=2)
print(result3)
输出:
['The', 'day', 'is', 'a', 'sunny', 'day']
['The da', ' is a sunn', ' da', '']
['The', 'day', 'is a sunny day']
partition 从左边开始找,根据指定的分隔符,返回一个元组,元组中包含:分隔符左侧内容,分隔符,分隔符右侧内容
info = 'sz-18-180-12345678'
result = info.partition('-')
result1 = info.partition('|')
print(result)
print(result1)
输出:
('sz', '-', '18-180-12345678')
('sz-18-180-12345678', '', '')
rpartition 从右边开始找,根据指定的分隔符,返回一个元组,元组中包含:分隔符左侧内容,分隔符,分隔符右侧内容
info = 'sz-18-180-12345678'
result = info.rpartition('-')
result1 = info.rpartition('|')
print(result)
print(result1)
输出:
('sz-18-180', '-', '12345678')
('', '', 'sz-18-180-12345678')
splitlines 按照行(’\r’, ‘\r\n’, \n’)切割
msg = 'hello,\neveryone\n haha\nddd'
result = msg.splitlines()
print(result)
输出:
['hello,', 'everyone', ' haha', 'ddd']
join 使用连接符,将列表中元素拼接成字符串
names = ['Bob','Tom','Sam']
names_new1 = '-'.join(names)
names_new2 = ' '.join(names)
names_new3 = ','.join(names)
print(names_new1)
print(names_new2)
print(names_new3)
输出:
Bob-Tom-Sam
Bob Tom Sam
Bob,Tom,Sam
五、判定:
isalpha 判断字符串中是否所有的字符都是字母
s = ' jack ma \n '
s1 = 'jack ma'
s2 = 'Jack'
s3 = 'jack25'
s4 = ''
s_new = s.isalpha()
s1_new = s1.isalpha()
s2_new = s2.isalpha()
s3_new = s3.isalpha()
s4_new = s4.isalpha()
print(s_new)
print(s1_new)
print(s2_new)
print(s3_new)
print(s4_new)
输出:
False
False
True
False
False
isdigit 判断字符串中是否所有的字符都是整数
n = '24'
m = '24.3'
result = n.isdigit()
result1 = m.isdigit()
print(result)
print(result1)
输出:
True
False
isalnum 判断字符串中是否所有的字符都是字母or数字
s = ' jack ma \n '
s1 = 'jack ma'
s2 = 'jack'
s3 = 'jack25'
s_new = s.isalnum()
s1_new = s1.isalnum()
s2_new = s2.isalnum()
s3_new = s3.isalnum()
print(s_new)
print(s1_new)
print(s2_new)
print(s3_new)
输出:
False
False
True
True
isspace 判断字符串中是否所有的字符都是空白符
s1 = ''
s2 = ' '
s3 = '\r'
s4 = '\n'
s1_new = s1.isspace()
s2_new = s2.isspace()
s3_new = s3.isspace()
s4_new = s4.isspace()
print(s1_new)
print(s2_new)
print(s3_new)
print(s4_new)
输出:
False
True
True
True
startswith 判断是否以XX开头
name = '2018-09-02:某某报告.xls'
print(name.startswith('2018-09-02'))
print(name.startswith('2018-09-03'))
print(name.startswith('2018',2))
print(name.startswith('18',2))
print(name.startswith('18',0,2))
print(name.startswith('18',2,4))
输出:
True
False
False
True
False
True
endswith 判断是否以XX结束
name = '2018-09-02:某某报告.xls'
print(name.endswith('.xls'))
输出:
True
|