I'm very new to python and have read what I could find on for loops and the del operator, but I still don't understand why my solution to the problem below isn't working:
PROBLEM
Given a string, return a new string made of every other char starting with the first, so "Hello" yields "Hlo".
string_bits('Hello') → 'Hlo'
string_bits('Hi') → 'H'
string_bits('Heeololeo') → 'Hello'
SOLUTION
def string_bits(str):
for i in range(len(str)):
if i % 2 != 0:
del[i]
return str
This just returns a replica of the original string and doesn't delete anything - why aren't the odd numbers in the range being deleted?
del[i]wouldn't be the correct syntax; you're deleting the namei, not a piece of the sequencestr. Also, don't name a variablestr.