Python3知识点:切片

基本语法:strstartendstep
 注意事项: 1.区间范围左闭右开
         2.start不写默认0
         3.end不写默认无限最后
         4.step为步长:step为正时,从左至右进行切片;step为负时,从右至左进行切片
 
  
#切片操作 s = 'abcdefg' print("切片操作的结果:",s[:1]) print("切片操作的结果:",s[::2]) print("切片操作的结果:",s[2:5]) print("切片操作的结果:",s[::-1]) #反转

运行结果如下:

猜你喜欢

转载自blog.csdn.net/fighting_yaya/article/details/80271792