-4

Write a function that takes as an input parameter a string and a number n and returns a newly created string made of every nth letter.

Can anyone help?

RohitWagh
  • 1,999
  • 3
  • 22
  • 43
  • 1
    What have you tried? Have you looked at any string methods, in particular `slice notation`? – sberry Nov 12 '11 at 02:19

1 Answers1

2

This is quite simple in Python because of the way it handles indexing:

def every_n(s, n):
    return s[::n]

That third item between the colons says "every n".

Check out Explain Python's slice notation for a more in depth description of how to slice in Python.

Community
  • 1
  • 1
Donald Miner
  • 38,889
  • 8
  • 95
  • 118