First, a list of sliced very basic, must remember the simplicity!
= test_list [1,2,3,4,5,6 ] # start with an index to index 2. 5 Print (test_list [2:. 5 ]) # start to the end of the index 3 from the Print (test_list [3 :]) # from the beginning to the index position 5 Print (test_list [: 5 ]) # from the beginning to the end of the Print (test_list [:]) # cyclic list print data for Item in test_list: Print (Item)
The results are:
[3, 4, 5]
[4, 5, 6]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
1
2
3
4
5
6
Under standard list [] is left to follow the package does not include the right principle, the subscript starts at 0
Second, the string may be sliced
= STR " People's Republic of China " Print (STR [. 1:. 5: 2 ]) # start with an index to index 2. 5 Print (STR [2:. 5 ]) # start to the end of the subscript 3 Print (STR [3 :]) # from the beginning to the index position 5 Print (STR [: 5])
The results are:
China and China
People's Republic of
China Republic
of China People's Republic
Wherein str [1: 5: 2], in brackets, is a first start position (including), a second end position (not included), a third interval value, that is, in the front range, every few take a value, the list also have this usage, the default is 1, this value is omitted if not written, then the final colon may be omitted