编程语言
首页 > 编程语言> > Python_字符串相关操作

Python_字符串相关操作

作者:互联网

1.字符串切片操作:

str1='hello word'
startIndex=0 #开始索引位置
endIndex=5   #结束索引位置+1
step=2       #步长
print(str1[startIndex:endIndex]) #结果:hello ,startIndex:开始索引位置,endIndex:结束索引位置+1

print(str1[:5]) #结果:hello,如果开始索引位置为0,可以省略不写

print(str1[:]) #结果:hello word ,如果开始位置为0,结束位置为当前字符串的长度,可以都省略不写

print(str1[startIndex:endIndex:step]) #结果:hlo,

print(str1[::step])  #结果:hlowr

 

标签:endIndex,索引,Python,str1,print,startIndex,字符串,操作,hello
来源: https://www.cnblogs.com/myfy/p/11451699.html