06 字符串索引与切片

Posted on 2018-09-09 19:50  白色雪狐  阅读(105)  评论(0编辑  收藏  举报
s = 'ABCDEFGHIJKLMN'
#索引
print(s[0])         #A
print(s[2])         #C
#切片 顾头不顾尾
print(s[0:3])       #ABC
print(s[0:])        #ABCDEFGHIJKLMN
print(s[:])         #ABCDEFGHIJKLMN
print(s[-1])        #N
print(s[:3])        #ABC
print(s[1:5:2])     #BD    首:尾:步长
print(s[4:0:-1])    #EDCB
print(s[4::-1])     #EDCBA
print(s[4::-2])     #ECA
print(s[-1::-1])    #NMLKJIHGFEDCBA
print(s[::-1])      #NMLKJIHGFEDCBA

 

Copyright © 2024 白色雪狐
Powered by .NET 8.0 on Kubernetes