python学习笔记3(切片+字符串常用操作方法)

切片

#如果选取方向(开始到结束的方向)与步长方向冲突,无法选取数据

str = '12345678'
print(str[1:7])   #234567  步长为1
print(str[1:7:2])   #246   步长为2
print(str[:5])  #12345   默认下标从0开始
print(str[4:])    #5678   默认取到最后
print(str[:])     #12345678  选取全部
print(str[::-1])   #87654321  倒序输出
print(str[-4:-1])  #567 下标-1表示最后一个数据

print(str[0:7:-1])#无输出
print(str[-4:-1:-1])#无输出
#如果选取方向(开始到结束的方向)与步长方向冲突,无法选取数据

字符串常用操作方法

查找:字符串查找方法即是查找子串在字符串中的位置或出现的次数

find():检测某个子串是否包含在这个字符串中,如果在返回这个子串开始的位置下标,否则返回-1
语法:字符串序列.find(子串, 开始位置下标, 结束位置下标)

mystr = "hello world and itcast and itheima and Python"
print(mystr.find('and')) # 12
print(mystr.find('and', 15, 30)) # 23
print(mystr.find('ands')) # -1

index():检测某个子串是否包含在这个字符串中,如果在返回这个子串开始的位置下标,否则报异常。
语法:字符串序列.index(子串, 开始位置下标, 结束位置下标)

mystr = "hello world and itcast and itheima and Python"
print(mystr.index('and')) # 12
print(mystr.index('and', 15, 30)) # 23
print(mystr.index('ands')) # 报错

rfind(): 和find()功能相同,但查找方向为右侧开始。
rindex():和index()功能相同,但查找方向为右侧开始。

count():返回某个子串在字符串中出现的次数
语法:字符串序列.count(子串, 开始位置下标, 结束位置下标)

mystr = "hello world and itcast and itheima and Python"
print(mystr.count('and')) # 3
print(mystr.count('ands')) # 0
print(mystr.count('and', 0, 20)) # 1

修改:所谓修改字符串,指的就是通过函数的形式修改字符串中的数据
replace():替换
语法:字符串序列.replace(旧子串,新子串,替换次数)

mystr = "hello world and itcast and itheima and Python"
# 结果: hello world he itcast he itheima he Python
print(mystr.replace('and', 'he'))
# 结果: hello world he itcast he itheima he Python
print(mystr.replace('and', 'he', 10))
# 结果: hello world and itcast and itheima and Python
print(mystr)

注意:数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时候不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型

spilt():按照指定字符分割字符串
语法:字符串序列.spilt(分割字符,num)

mystr = "hello world and itcast and itheima and Python"
# 结果: ['hello world ', ' itcast ', ' itheima ', ' Python']
print(mystr.split('and'))
# 结果: ['hello world ', ' itcast ', ' itheima and Python']
print(mystr.split('and', 2))
# 结果: ['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(mystr.split(' '))
# 结果: ['hello', 'world', 'and itcast and itheima and Python']
print(mystr.split(' ', 2))

join():用⼀个字符或子串合并字符串,即是将多个字符串合并为一个新的字符串。
语法:字符或子串.join(多字符串组成的序列)

list1 = ['chuan', 'zhi', 'bo', 'ke']
t1 = ('aa', 'b', 'cc', 'ddd')
# 结果: chuan_zhi_bo_ke
print('_'.join(list1))
# 结果: aa...b...cc...ddd
print('...'.join(t1))

capitalize():将字符串第⼀个字符转换成大写
title():将字符串每个单词首字母转换成大写
lower():将字符串中大写转小写
upper():将字符串中小写转大写
lstrip():删除字符串左侧空白字符
rstrip():删除字符串右侧空白字符
strip():删除字符串两侧空白字符
ljust():返回⼀一个原字符串左对齐,并使用指定字符(默认空格)填充至对应长度的新字符串。
语法:字符串序列.ljust(长度,填充字符)

mystr = 'hello'
print(mystr.ljust(10,'.'))  #'hello.....'
print(mystr.ljust(10))      #'hello     '

rjust():返回⼀个原字符串右对齐,并使⽤用指定字符(默认空格)填充⾄至对应长度 的新字符串串,语法和ljust()相同。
center():返回⼀个原字符串居中对齐,并使用指定字符(默认空格)填充至对应长度的新字符串,语法和ljust()相同。

mystr = 'hello'
print(mystr.center(10,'.'))  #'..hello...'
print(mystr.center(10))      #'  hello   '

判断:所谓判断即是判断真假,返回的结果是布尔型数据类型: True 或 False。
startswith():检查字符串是否是以指定子串开头,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。
语法:字符串串序列.startswith(⼦串, 开始位置下标, 结束位置下标)

mystr = "hello world and itcast and itheima and Python "
# 结果: True
print(mystr.startswith('hello'))
# 结果False
print(mystr.startswith('hello', 5, 20))

endswith()::检查字符串是否是以指定子串结尾,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。语法与startwith相同。

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

mystr1 = 'hello'
mystr2 = 'hello12345'
# 结果: True
print(mystr1.isalpha())
# 结果: False
print(mystr2.isalpha()

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值