-1

For a homework problem, one of my functions requires a dash to be added at a certain index. For instance, with a sequence such as "ABC" - at index 2 of a new string (as per Python rules) - the string would ideally output "AB-C." So, index 2 of a new string.

(code provided) I attempted to set aside any part of the phrase before where the dash would be placed, and played around with trying to add the dash at index 2 with my 2nd line. I know I need a new string, but I am otherwise confused about how to create a new string and be able to place the dash at the desired index.

def insert_indel(sequence, index):

    head = sequence[:index]
    tail = sequence[ index+1: ]
    return head + '-' + tail

v = insert_indel('ABC', 1)
print(v)

I only am outputting "A-G" - it is substituting a dash at the 2nd index of the old string instead of putting it onto a new string.

1 Answers1

0

Is this what you are looking for?:

def insert_indel(sequence, index):

    head = sequence[:index]
    tail = sequence[ index: ]
    return head + '-' + tail

v = insert_indel('ABC', 2)
print(v)

Input: ABC 2

Output: AB-C

Qazi Fahim Farhan
  • 2,066
  • 1
  • 14
  • 26