Python字符串操作

本文介绍了Python中字符串的基本操作,如切片、遍历、查找子串(find和count)、替换(replace)、分割(split)、大小写转换(capitalize、title、lower、upper)以及字符串的边界检查和空白处理。同时提到Python字符串是不可变的,对于可变字符串,提到了StringIO和array模块的使用。
摘要由CSDN通过智能技术生成

切片

切片是指对操作的对象截取其中一部分的操作,字符串,列表,元组都支持切片
语法:字符串[开始位置下标:结束位置下标:步长]
注意:选取的区间属于左闭右开型,即从"起始"位开始,到"结束"位的前一位结束(不包含结束位本身),步长默认是1。步长是控制方向的,正数从左往右取,负数是从右到左取我们以字符串为例讲解

name = 'testfan'
print(name[2:5:1]) # 下标25,步长是1  stf
print(name[2:5]) # 默认步长是1  stf
print(name[:5]) # 不写起始,则认为是从头开始   testf
print(name[2:5:2]) # 步长为2就后面元素的索引-前面的索引=步长  sf
print(name[::]) # testfan
print(name[:]) # testfan
print(name[-5:-1]) # 倒叙截取,从-5个字符(重点)  stfa

遍历

var="fskakfskl"
for i in var:
	print(i)

查找

find():检测某个字符串是否在某个字符串中,如果在,就返回子串开始位置的下标,否则,返回-1
语法:字符串序列.find(子串,开始位置下标,结束位置下标)
注意:开始和结束位置下标可以省略,表示在整个字符串序列当中查找

str= 'hello world and testfan and yaoyao' #12
print(str.find('and')) # 返回的是字串在字符串当中的开始位置的索引  12
print(str.find('and',15,-1)) #24
print(str.find('ands')) # -1

count():获取字符串中某个字串个数

print(str.count('and'))#2

len():获取字符串长度

修改

replace():替换
语法:字符串序列.replace(旧子串,新子串,替换次数)
注意:替换次数如果超出子串出现的次数,则替换次数为该子串出现的次数。字符串是不可变类型,修改字符串不会修改原字符串,而是新建了一个字符串返回

str = 'hello world and testfan and yaoyao'
print(str) # 字符串是不可变类型,所以不会改变原来数据的结构
# 结果:hello world and testfan and yaoyao
print(str.replace('yaoyao','python'))
# 结果:hello world and testfan and python
print(str.replace('and','2')) # 默认是更改所有的
# 结果:hello world 2 testfan 2 yaoyao
print(str.replace('and','2',1))
# 结果:hello world 2 testfan and yaoyao
print(str.replace('and','2',3))
# 结果:hello world 2 testfan 2 yaoyao

split(): 按照指定字符分隔字符串
语法:字符串序列.split(分割字符,num)
注意:num表示的是分隔字符出现的次数

str = 'hello world and testfan and yaoyao'
print(str.split(' ')) # 经过split()分隔之后就是一个列表
# 结果:['hello', 'world', 'and', 'testfan', 'and', 'yaoyao']
print(str.split(' ', 2))
# 结果:['hello', 'world', 'and testfan and yaoyao']
print(str.split('and')) # 如果分隔符是字符串序列的一个子串,那么就会丢失
# 结果: ['hello world ', ' testfan ', ' yaoyao']

capitalize(): 将字符串第一个字符串转换成大写
title() :将字符串每个单词首字母转换成大写
lower(): 将字符串中大写转小写
upper():将字符串中小写转大写
lstrip():删除字符串左侧空白字符

str = ' hello world and testfan and yaoyao'
print(str)
print(str.lstrip())#输出hello world and testfan and yaoyao

rstrip():删除字符串右侧空白字符
strip():删除字符串两侧空白字符

判断

startswith():检查字符串是否是以指定子串开头,是则返回True,否则返回False。如果设置开始和结束位置下标,则在指定范围内检查
语法:字符串序列.startswith(子串,开始位置下标,结束位置下标)

str1 = 'helloworld11111'
print(str1.startswith('hell')) # True
print(str1.startswith('hello',5,-1)) #False

endswith(): 检查字符串是否是以指定子串结尾,是则返回True,否则返回False。
语法:字符串序列.endswith(子串,开始位置下标,结束位置下标)

isalpha():如果字符串至少有一个字符并且所有的字符都是字母则返回True,否则返回False

str1.isalpha()

isdigit():如果字符串只包含数字则返回True,否则返回False
isalnum():如果字符串中至少有一个字符并且所有的字符是字母或者数字则返回True,否则返回False
isspace(): 如果字符串中只包含空白,则返回True,否则返回False

可变字符串

字符串对象是不可变的,如果需要创建可变字符串对象,需要使用io.StringIO对象或array模块

s="fsajkfas;fa"
sio=io.StringIO(s)#sio就是可变字符串了
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力码代码的小赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值