#字符串操作 string[start:end:step] start默认0 end默认列尾 step默认1
s="abcdefghijk"
print("s---",s)
print("s[0:2]---",s[0:2])#string[start:end]
print("s[:2]---",s[:2])#string[:end] start默认0,step默认1
print("s[2:]---",s[2:])#string[start:] start为2,end默认列尾,step默认1
print("s[2,6]---",s[2:6])#string[start:end] start为2,end为6,step默认1
print("s[:]---",s[:])#string[:] start默认0,end默认列尾,step默认1
print("s[::2]---",s[::2])#string[::step] start默认0,end默认列尾,step为2
print("s[0:7:2]---",s[0:7:2])#string[:end:step] start默认0,end为7,step为2
'''
s="abcdefghijk"
print(s[7::-1])#hgfedcba step<0 索引减小的 start=7 stop=0 step=-1
print(s[7:-1:])#hij start=7 stop=len(s)-1 step=1
print(s[-1:])#k start=len(s)-1 stop=len(s) step=1
print(s[:-1:])#abcdefghij start=0 stop=len(s)-1 step=1
print(s[7:-1:-1])# start=7 stop=len(s)-1 step=-1--->stop=0 error
print(s[8:0:-1])#ihgfedcb start=8 stop=0 step=-1--->stop=0
print(s[5:1:-2])#fd start=5 stop=1 step=-2--->stop=1
print(s[4:1:-2])# start=4 stop=1 step=-2-->stop=1
print(s[7:])
print(s[7:-1])
print(s[7:-1:1])
print(s[::-2])
print(s[:])
print(s[-2:6])
print(s[-2])
print(s[-2:])
print(s[:-2])
print(s[0:-2])
python基础复习(30)--string[start:end:step] start默认0 end默认列尾 step默认1
猜你喜欢
转载自blog.csdn.net/xt18971492243/article/details/112258783
今日推荐
周排行