⊗pyPmSlSt 81 of 208 menu

Sampling step for slicing in Python

If we need to select elements with a certain step, then we should specify a number in the third parameter of the slice. Let's select every second character except the first and last:

txt = '123456789' res = txt[1:9:2] print(res) # '2468'

Given a string:

txt = '123456789'

Get a slice of all its characters at odd positions:

'13579'

The following code is given:

txt = '123456789' res = txt[1:-1:2] print(res)

Write what is output to the console.

Given a list:

lst = ['a', 'b', 'c', 'd', 'e']

Get the following slice from it:

['a', 'd']
byenru