-5

Here is a string as an example.

s = 'asdf df d f d ssa'.

I need to get words at even indices from a string. For the above string s, the words are:

1. 'asdf'
2. 'df'   // Even index
3. 'd'
4. 'f'    // Even index
5. 'd'
6. 'ssa'  // Even index

A correct output would be 'df f ssa'. I think I would do this with a slice.

How might I go about this?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
pshx
  • 19
  • 2

1 Answers1

3

You meant words at even indices (if that sounds correct). split and then slice with a step of 2 starting at 1:

>>> ' '.join(s.split()[1::2])
'df f ssa'

Your even would mean odd here, since indexing starts from zero.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139