-2

Why this doesn't work for removing characters from strings in python?

  return str[n]= "" 

and we are forced to use this:

  front = str[:n]   # up to but not including n
  back = str[n+1:]  # n+1 through end of string
  return front + back

The Problem: Given a non-empty string and an int n, return a new string where the char at index n has been removed. The value of n will be a valid index of a char in the original string (i.e. n will be in the range 0..len(str)-1 inclusive).

missing_char('kitten', 1) → 'ktten'
missing_char('kitten', 0) → 'itten'
missing_char('kitten', 4) → 'kittn'

enter image description here

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70

1 Answers1

1

one way of doing it is by iterating over all letters with the index via the enumerate method:

def remove_index_from(example_string):
    return "".join([letter for index,letter in enumerate(example_string) if index != index_to_remove])
lhd
  • 436
  • 4
  • 16