字符串的常用方法---python05
字符串的特点
不可变
可以下标访问
1. 字符串的查找替换统计
find() | 找不到子串的时候返回-1,找到返回第一个字符的下标 ***** |
---|---|
rfind() | 了解 从右边开始找 |
index() | 找不到程序会崩溃 |
rindex () | 了解 |
repalce() | 默认全部替换 ***** |
count() | 统计出现的次数 ***** |
案例:
s = 'hello World hello world hello python'
def test_find():
id = s.find('orld')
# 返回的是第一个字符的下标
print(id)
# 从下标7开始找 12结束,不包括12下标的字符
idx = s.find('o',7,12)
print(idx)
# test_find()
def test_index():
id = s.index('orld')
print(id)
# 找不到 ValueError: substring not found,程序就死了
idx = s.index('o',7,12)
print(idx)
# test_index()
def test_rfind():
print(s.rfind('o'))
print(s.rindex('o'))
print(s.rfind('kk'))
# 没有就报错ValueError: substring not found 程序奔溃
# print(s.rindex('kk'))
print(len(s))
test_rfind()
# replace替换
def test_replace():
# 默认替换所有符合的字符串
print(s.replace('o','O'))
print(s)
# 只替换三个
print(s.replace('o','O',3))
# test_replace()
# count计数
def test_count():
print(s.count('o'))
print(s.count('o',8,20))
test_count()
2.分割与连接
split() ***** | 输出的结果是列表,要有分隔符,每个都会生效,以什么分割会被删除掉,可以设置分割的次数 |
---|---|
splitlines() | 只识别\n |
partition() | 只会分割成三部分,并且分割的部分不会删掉 |
rpatirion() | 从右边开始分割 |
join() ***** | 加入字符进行连接列表中的每个元素 |
案例:
s = 'hello wprld yq hello python'
def test_split():
#分割的部分会消失
print(s.split(' '))
#分割的最大的次数
print(s.split('o',1))
#按照行进行分割
ss = 'hello \nworld hello wprld \npython'
print(ss.splitlines())
print(ss.split('\n'))
#只要有空白就分割,spli不加参数
sss = 'hello \nworld \thello wprld \npython'
print(sss.split())
# test_split()
# 分割三个部分
def test_partition():
s = 'hellohahawprldhahapython'
#保留分割的部分
print(s.partition('haha'))
# 从右边进行分割
print(s.rpartition('haha'))
# test_partition()
# join
def test_join():
s = 'hello world'
#每一个字符之间都加入_
print('_'.join(s))
#注意区分字符串与列表元素的区别,list中不只是一个单独的字符
ss = s.split(' ')
print(ss)
print('-'.join(ss))
test_join()
3. 字符串的判断,返回得类型bool
startswith() ***** | 判断以什么指定的字符串开头 |
---|---|
endswith() ***** | 以什么结尾 |
isupper() | 是否全部大写 |
islower() | 是否全部小写 |
isdigit() | 是否全是数字 |
isalpha() | 是否全是字母 |
isalnum() | 字母或者数字,但是不包括字符 |
isspace | 是不是空百字符,包括空格\n \t \t |
案例:
s = '13526091932'
def test_startswith():
print(s.startswith('135'))
print(s.endswith('2'))
print('www.xxx.gov'.endswith('gov'))
# test_startswith()
def test_other():
print('Hello'.isupper())
print('HELLO'.isupper())
print('hello'.islower())
print('123'.isdigit())
print('123abc'.isdigit())
print('abc'.isalpha())
print('abc123'.isalpha())
print('123'.isalnum())
print('123abc'.isalnum())
print('123 abc'.isalnum())
# "是一个特殊的字符串,不是空格
print(''.isspace())
print(' '.isspace())
print('\t'.isspace())
print('\n'.isspace())
test_other()
4. 字符串的转换
upper() | 所有的转换成大写 |
---|---|
lower() | 全部转换成小写 |
title() | 将每个单词的首字符转换成大写,注意不会自动识别单词 |
capitalize() | 将第一个单词的首字符转换成大写 |
案例:
def test():
s = 'hello WORLD'
print(s.upper())
print(s.lower())
print(s.title())
print(s.capitalize())
test()
5. 字符串的对齐
注意要给定宽度,需要参数
center() | 按照给定的宽度居中显示 |
---|---|
rjust() | 右对齐 |
ljust() | 左对齐 |
案例:
s = 'hello world'
def test():
print(s.center(20))
print(s.center(20,'_'))
# 默认填充空白
print(s.rjust(20))
print(s.ljust(20))
test()
6. 去除空白
strip() | 去除两端的空白 |
---|---|
lstrip() | 去除左侧的空白 |
rstrip() | 去除右边的空白 |
案例:
def test():
s = ' hello world 1 '
print(s)
print(s.strip())
print(s.lstrip())
print(s.rstrip())
test()