Python: Practice Strings Quiz

Hello everyone:

LInk: Python String Quiz

The instruction:

Write a function called gregs_e_tagger that takes a string as an argument and inserts “greg” in the middle of the string, and then returns the new “greg-i-fied” string.
For odd length strings, consider the “middle” to be left of the middle character.


def gregs_e_tagger(lst):
    lst.insert(len(lst) //2, "Greg")
    return lst

print(gregs_e_tagger([1,  2, 3, 4,  5]))
print(gregs_e_tagger(["one", "two", "three", "four"]))

I get a message that states, “‘str’ object has no attribute ‘insert’” - However, my code does return the requested objective but it will submit as a solution. Any advice would be greatly appreciated - Thank you in advanced for any help!

sure this is accurate? I get taken to a review section.

that takes a string as an argument the description says. The exercise seem to test with strings by calling your function with string arguments, while your calls pass list as arguments

1 Like

Hey Stetim94,

Yes, that is the right link. It was a question to a quiz, so it may not take you to the exact page, my apologizes. I could screen shot it if that would help?

that would help. But my point about you using lists where the exercise seems to expect strings still stands

Roger that - Got it! Thanks for your help!

Hi, I’ve just been doing the same practice quiz and it won’t let me pass this question. My code seems to work correctly when I’ve tested it, but when I check my answer I’m getting " gregs_e_tagger did not correctly insert "greg" into the middle of the string".

Here’s my code:

def gregs_e_tagger(string):
  middle = int(len(string) / 2)
  split_string = string.split(string[middle])
  return split_string[0] + "greg" + string[middle] + split_string[1]

Is my code correct and is there a better way of writing this function?

Can you link the exercise? And can i see the full error message?

It was a question on one of the 10 minute practice segments on my dashboard but I can’t get back into it to link it now, it’s just taking me into different practice questions (same thing when following seraph76’s link in the original post).

There wasn’t an error message generated by the code itself, I just got

when trying to check my answer and there wasn’t an option to view the correct solution.


I don’t know what corner cases the exercise tests, but an empty string:


would be problematic.

Ok, it could potentially be that, thanks.

actually, there are more corner cases in your code:

def gregs_e_tagger(string):
  middle = int(len(string) / 2)
  split_string = string.split(string[middle])
  return split_string[0] + "greg" + string[middle] + split_string[1]


I have never done this exercise, so i don’t know what cases the exercise test for.

So the exercise could be tripping on something like that.

1 Like

Ah yeah, you’re right repeated characters in the string would mess it up. I’m struggling with a finding a solution to get around this!

even repeated non-successive characters.

split is used to separate based on certain delimiter. Which is not really what we want here, we want to slice based on the middle index.

What do we have for this?

String slicing! :smile: I was having trouble of thinking how to do this, but I think I’ve got it now. This seems to work:

def gregs_e_tagger(string):
  return string[:int(len(string) / 2)] + "greg" + string[int(len(string) / 2):]

1 Like

Would this still work if the length of the string is not divisible by 2?

Did you test the function? It works with even or odd length inputs. The crucial part is int() so we don’t raise a TypeError using a float in a slice.

Given that both numerator and denominator are integers we could use floor division.

>>> def gregs_e_tagger(s):
  return f"{s[:len(s) // 2]}greg{s[len(s) // 2:]}"

>>> gregs_e_tagger("beeeeeees")
>>> gregs_e_tagger("beeeeees")

Thank you! It does work- I’ll keep that in mind regarding int()

Why is it that the larger half is on the right side of the inserted text?

Thanks again, I really appreciate your help.

1 Like
9 // 2 => 4

b e e e

with 4 e’s left on the right. The continuation is from index 4.

beee e e e e s
1 Like