python之字符串

目录

定义方式

角标

切片

字符串查找函数

字符串修改

字符串分隔 split

‘字符’.join(序列)

capitalize仅开头字母大写

title 单词首字母大写

ljust rjust center 凑字符串的位数   这个方向是针对原字符串的位置展示

startwith endwith 

isxxx是否都是


定义方式

单引号 双引号 三个单引号 三个双引号 三引号输出的和书写格式保持一致

str1='''nihao
ye'''
print(str1)
# nihao
# ye

角标

str1='nihaoye'
print(str1[2])
#h

切片

就是对对象进行部分截取

字符串、列表、元组都⽀持  毕竟都存在角标一说

序列[开始位置下标:结束位置下标:步⻓]

1. 不包含结束位置下标对应的数据, 正负整数均可; 包含头不包含尾

2. 步⻓是选取间隔,正负整数均可,默认步⻓为1。

str1='0123456789'
print(str1[2:]) #23456789  包含2之后的所有
print(str1[2:5]) #234   包含2不包含5之间的
print(str1[:5]) #01234 从头到不包含5之间的
print(str1[2:10:3]) # 258 包含2不包含10 之间 每选择一次就跳3步
print(str1[:]) #原样输出
print(str1[::2]) #02468
print(str1[::-1])#9876543210
print(str1[:-1])#012345678
print(str1[-4:]) #6789
print(str1[-4:-1]) #678
print(str1[-4:-1:-1])  #没结果 找不到 因为-4 -1虽然是倒着找但是是正方形截取, -1跳步 是倒着走
print(str1[-4:1:-1])  #65432  -4倒着找的 1是正着找的 之间的方向也是倒着的 -1跳步也是倒着 所以能找到 
# 负数的话就倒着选取
# 切记倒着选取的时候不是从0开始 而是-1
#有些绕  费脑子

字符串查找函数

str.find(字串,[开始的index,结束的index])  存在返回位置即角标 不存在返回-1

str.index(字串,[开始的index,结束的index]) 存在返回位置即角标 不存在报错

str.count(字串,[开始的index,结束的index]) 出现的次数 

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

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

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
#print(mystr.index('ands'))  #ValueError: substring not found
print(mystr.count('and')) #3次
print(mystr.count('ands')) #0
print(mystr.count('and',15,30)) #1

字符串修改

str.replace('old','new',[次数])  吧老串换成新串 依据换几次

此函数不能修改原字符串

mystr = "hello world and itcast and itheima and Python"
print(mystr.replace('and','he'))
print(mystr.replace('and','he',2))
print(mystr)
# hello world he itcast he itheima he Python
# hello world he itcast he itheima and Python
# hello world and itcast and itheima and Python

字符串分隔 split

str.split('分隔字符',[次数]) 返回列表数组  注意该分隔字符 再字符串中必须存在

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

join带有角标的序列以某字符为分隔,转成字符串

‘字符’.join(序列)

mystr = "hello world and itcast and itheima and Python"
print('-'.join(mystr))
list1 = ['chuan', 'zhi', 'bo', 'ke']
print('_'.join(list1))
t1 = ('aa', 'b', 'cc', 'ddd')
print('...'.join(t1))
# h-e-l-l-o- -w-o-r-l-d- -a-n-d- -i-t-c-a-s-t- -a-n-d- -i-t-h-e-i-m-a- -a-n-d- -P-y-t-h-o-n
# chuan_zhi_bo_ke
# aa...b...cc...ddd

capitalize仅开头字母大写

整个字符串 的仅仅是第一个字母大写 ,其他转为小写

mystr = "hello world and itcast and itheima and Python"
print(mystr.capitalize())
# Hello world and itcast and itheima and python

title 单词首字母大写

每个单词的开头字母都大写,其他转为小写

mystr = "helloworld and itCast and itheima and Python"
print(mystr.title())
# Helloworld And Itcast And Itheima And Python

lower() 都转小写 upper()都转大写

strip 去除左右2边的空格

mystr = "    helloworld and itCast and itheima and Python    "
print(mystr.lstrip()) #去除左边
print(mystr.rstrip()) #去除右边
print(mystr.strip()) #都去除

ljust rjust center 凑字符串的位数   这个方向是针对原字符串的位置展示

str.ljust(要凑的位数,[凑出位数多余的用什么表示,默认是空格])

mystr = "helloworld and itCast and itheima and Python"
print(mystr.ljust(50)) #去除左边
print(mystr.rjust(50,',')) #去除右边
print(mystr.center(50,'-'))#居中
# helloworld and itCast and itheima and Python      
# ,,,,,,helloworld and itCast and itheima and Python
# ---helloworld and itCast and itheima and Python---

startwith endwith 

表示是否以某字符串开头或者结尾  返回bool

str.startwith('字符串',[startindex,endindex])

str.endwith('字符串',[startindex,endindex])

mystr = "helloworld and itCast and itheima and Python"
print(mystr.startswith('hello')) #True
print(mystr.startswith('and',11,14))#True
print(mystr.endswith('python')) #False
print(mystr.endswith('Python',38,44)) #True
print(len(mystr)) #44

isxxx是否都是

isalpha()  是否都是字母 str.isalpha()

isdigit() 是否都是数字

isalnum() 是否只包含数字或字母

isspace() 该字符串是否只有空格

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值