# 序列
# 对list进行切片,从1开始4结束,步长为1
my_list = [0, 1, 2, 3, 4, 5, 6]
result = my_list[1:4]
print(f"结果1:{result}")
# 对tuple 进行切片,从头开始到最后结束,步长为1
my_tuple = (0, 1, 2, 3, 4, 5, 6)
result1 = my_tuple[:] # 起始可以不写,用:就行,步长为1可以不写
print(f"结果2:{result1}")
# 对str进行切片,从头开始到最后结束步长为2
my_str = "abcdefghijklmn"
result3 = my_str[::2]
print(f"结果3为{result3}")
# 对str进行切片,从头开始到最后结束步长为-1
my_str = "abcdefghijklmn"
result4 = my_str[::-1] #等于吧字符串翻转了
print(f"结果4为{result4}")
# 对list进行切片,从3开始1结束,步长为-1
my_list = [0, 1, 2, 3, 4, 5, 6]
result5 = my_list[3:1:-1]
print(f"结果5:{result5}")
# 对tuple 进行切片,从头开始到最后结束,步长为-2
my_tuple = (0, 1, 2, 3, 4, 5, 6)
result6 = my_tuple[::-2]
print(f"结果6:{result6}")
运行结果:
结果1:[1, 2, 3]
结果2:(0, 1, 2, 3, 4, 5, 6)
结果3为acegikm
结果4为nmlkjihgfedcba
结果5:[3, 2]
结果6:(6, 4, 2, 0)