字符串常见操作

字符串常见操作

获取长度:len()

查找内容:find,index,rfind,rindex

find:从左向右查找,只要遇到一个符合要求的则返回其下标位置,如果没有找到则返回-1

例如:

path = 'https://www.baidu.com/img/dong_scssfqwewqewqqw123wdqe12ew1.jpg'
i = path.find('_')
i2 = path.find('.jpg')
#find返回的是当前字符串所在下标,所以要下标+1
print(path[i+1:i2])

输出:

scssfqwewqewqqw123wdqe12ew1

rfind:right find,从右向左查找,只要遇到一个符合要求的则返回其下标位置,如果没有找到则返回-1

例如:

path = 'https://www.baidu.com/img/dong_scssfqwewqewqqw123wdqe12ew1.jpg'
i = path.rfind('.')
print(path[i:])

输出:

.jpg

index:和find类似,从左往右查找指定字符串,找到则返回其下标位置,但是找不到会报错
rindex:和rfind类似,从右往左查找指定字符串,找到则返回其下标位置,但是找不到会报错

判断:

startswith、endswith、isalpha、isdigit、isalnum、isspace、isupper

判断字符串返回值都是布尔类型(True、False)

startswith:判断是否以指定字符串开头
s = 'dong_scssfqwewqewqqw123wdqe12ew1.jpg'
result = s.startswith('abc')
print(result)

输出:
False

endswith:判断是否以指定字符串结尾
s = 'dong_scssfqwewqewqqw123wdqe12ew1.jpg'
result = s.endswith('.jpg')
print(result)

输出:
True

isalpha:判断是否字母
s = 'qwEsdnDi'
result = s.isalpha()
print(result)
#输出:
True
isdigit:判断是否数字
s = '1321864'
result = s.isdigit()
print(result)
#输出
True
isalnum:判断是否数字或字母
s = '123abc'
result = s.isalnum()
print(result)
#输出
True
isspace:判断是否全空格
s = '     '
result = s.isspace()
print(result)
#输出
True
issupper:判断是否全大写
s = 'QWESAcvv'
result = s.isupper()
print(result)
#输出
False
islower:判断是否全小写
s = 'smocn'
result = s.islower()
print(result)
#输出
True

注意:都是判断的字符串

计算出现次数:count

count:计算字符串出现的次数

path = 'https://www.baidu.com/img/dong_scssfqwewqewqqw123wdqe12ew1.jpg'
n = path.count('.')
print(n)
#输出
3
替换内容:replace

replace:替换内容,replace('old str','new str',count),count代表替换次数,默认是全部替换。可以用正则表达式替换

s = '王二麻子说:李四你去唱歌,张三去跳舞吧!'
result = s.replace('李四','老王',1)
#1表示替换1次
print(result)
#输出
王二麻子说:老王你去唱歌,张三去跳舞吧!
切割字符串:split、resplit、splitlines、partition、repartition
splitsplit('分隔符',maxsplit),返回结果是一个列表,maxsplit表示最多分割次数
s = '张三,李四,王五,尼古拉斯大熊,伊丽莎白静香'
result = s.split(',',2)
#不写分割次数默认全分割
print(result)
#输出
['张三', '李四', '王五,尼古拉斯大熊,伊丽莎白静香']
#split分割成一个的列表,python里面叫做列表。
rsplit:同split,rsplit只是从右到左分割
splitlines:同split,只不过是按行切割
s = '''王二麻子你去学习吧!
张三你去跳舞吧!
李四你去唱歌吧!
王五你去吃饭吧!'''
result = s.splitlines()
print(result)
#输出
['王二麻子你去学习吧!','张三你去跳舞吧!','李四你去唱歌吧!','王五你去吃饭吧!']
partition:分隔符也带入
s = '王八犊子,麻烦你圆润的走开!'
result = s.partition(',')
print(result)
#输出:
('王八犊子', ',', '麻烦你圆润的走开!') 
修改大小写:capitalize,title,upper,lower
title:每个单词首字母大写
s = 'HELLO WORD!'
result = s.title()
print(result)
#输出
Hello Word!
upper:所有字母转换成大写字母,数字不影响
s = 'hello world1'
result = s.upper()
print(result)
#输出
HELLO WORLD1
lower:所有字母转换成小写字母,数字不影响
s = 'HELLO world123'
result = s.lower()
print(result)
#输出:
hello world123
capitalize:一句话的第一个字母大写,其他字母统统转换成小写。
s = 'hello WORLd'
result = s.capitalize()
print(result)
#输出:
Hello,world
空格处理:

ljust,rjust,center:添加空格控制字符串的对齐方式。
lstrip,strip,rstrip:删除字符串左侧或者右侧的空格

strip:删除字符串的头尾空格,返回一个字符串

s = 'admin   '
print(len(s))
result = s.strip()
print(len(result))
#输出
8
5

rstrip:只去除右侧空格

s = '   admin    '
print(len(s))
result = s.rstrip()
print(len(result))
print(result)
#输出
12
8
  admin

lstrip:只去除左侧空格

s = '   admin   '
result = s.lstrip()
print(len(s))
print(len(result))
#输出
11
8

center:给个整体字符长度,输出的字符串位于整体字符的中间位置

s = 'hello word!'
result = s.center(30)
print(result)
#输出:
        hello word!          

ljust:给出整体字符长度,输出的字符串位于整体字符的左侧位置

s = 'hello word!'
result = s.ljust(30)
print(result)
print(len(result))
#输出:
hello word!                   
30

rjust:给出整体字符长度,输出的字符串位于整体字符的右侧位置

s = 'hello word!'
result = s.rjust(30)
print(result)
#输出:
                  hello word!
字符串拼接:join

join:将整个join中的字符,元组,列表一个个取出,并在后面添加指定的字符,join中最后的字符保留,不加指定字符

s = ','
a = '。'
b = '、'
result = s.join(['hello', 'world', '我是李四!'])
print(result)
result = a.join(['hello', 'world', '我是李四!'])
print(result)
result = b.join(['hello', 'world', '我是李四!'])
print(result)
#输出:
hello,world,我是李四!
hello。world。我是李四!
hello、world、我是李四!

注:取出列表中的字符串,并在每个字符串后面将s中的字符进行拼接,最后的字符串不拼接。

字符串格式化

%d %s %f

s = '您好,我是王五!'
f = 666
d = 123
print('王五说:%s' % s)
print('王五说:%f' % f)
print('王五说:%d' % d)
#输出:
王五说:您好,我是王五!
王五说:666.000000
王五说:123
format:通过字符串中的{}来识别替换字段,从而完成字符串的格式化

省略字段名方法:

name = '老六'
age = 66
result = '{}今年{}岁'.format(name,age)
print(result)
#输出
老六今年66

数字字段名:数字填充

name = '老六'
age = 66
result = '{0}今年{1}岁,老王也是{1}岁。'.format(name,age)
print(result)
#输出
老六今年66岁,老王也是66岁。

变量字段名:format的参数必须是关键字参数
关键字参数:必须直接指定变量及变量参数。

result = '{name}今年{age}岁,老王也是{age}岁。'.format(name = '老六',age = 66)
print(result)
#输出
老六今年66岁,老王也是66岁。
注:在python中,字符串是不可变的!所有字符串相关方法,都不会改变原有字符串,都是返回一个结果,在这个新的返回值里,保留了执行后的结果。
扩展:生成随机的数字字母组合的6位随机数
import random
#这是导入random的函数

fileName = ''
s = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789'
for i in range(6):	#range(6)i取到从0-6的数字,也就是循环6次
	index = random.randint(0,len(s)-1)
	fileName += s[index]
print(fileName)
#输出:
随机六位数数字和大小写字母组合数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值