I wrote a function, that returns a new string made of every second character starting with the first.
So that for example "Pizza" yields "Pza", "Maogtbhdewr" yields "Mother".
This code is with range()
and len()
:
def string_skip(string):
new_string = ""
for n in range(0, len(string)):
if n % 2 == 0:
new_string += string[n]
return new_string
The code above works as expected. But pylint
shows an info suggesting to use enumerate()
instead of iterating with range()
and len()
. So, I tried to rewrite my code without the two built-ins, and I've also read through the documentaion. But I still could not get it to work.
My question: How do I use enumerate()
instead of using range()
and len()
?
yes you can, strings are iterables too.
def string_skip(string):
new_string = ""
for i, n in enumerate(string):
if i % 2 == 0:
new_string += n
return new_string