⊗pyPmSlOSt 82 of 208 menu

Slice with only sampling step in Python

You can also specify a step for the selection without specifying the start and end of the slice. To do this, omit the numbers indicating the start and end indices, and leave only the colons and the step size:

txt = '123456789' res = txt[::2] print(res) # '13579'

Given a string:

txt = '123456789'

Get a slice of all its symbols at even positions:

'2468'

Given a list:

lst = [1, 2, 3, 4, 5, 6, 7]

Get the following slice from it:

[1, 3, 5, 7]

Make a tuple of days of the week. Get a slice of weekdays and a slice of weekend days.

byenru