python 中列表、字符串、元组同时截取首位的序列

 

001、

>>> test1 = ["a", "b", "c", "d", "e", "f", "g"]      ## 测试列表
>>> test1
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> test1[2:-2]                                     ## 同时删除首尾的两个字符
['c', 'd', 'e']
>>> str1 = "abcdefg"                                ## 测试字符串
>>> str1
'abcdefg'
>>> str1[2:-1]                                      ## 删除字符串前两个字符、最后一个字符
'cdef'
>>> tuple1 = (1, 2, 3, 4, 5, 6, 7)
>>> tuple1[3:]
(4, 5, 6, 7)
>>> tuple1[2:-2]                                    ## 应用于元组
(3, 4, 5)

 

posted @ 2022-08-15 13:44  小鲨鱼2018  阅读(32)  评论(0编辑  收藏  举报