# Python: Practice Strings Quiz

I didn’t use int() at first but after the exercise rejected my code many times, I decided to try it even though it seemed futile to me.

Isn’t what the code supposed to be doing “str” + “greg” + “ing” and “stri” + “greg” + “ngs”?

Thank you so much for your quick response. I haven’t felt this confused in a long while

1 Like

So we can do the preliminary steps that will apply to both cases…

``````  n = len(string)                         # n is int
m = n // 2                              # floor divide for int
m += n % 2                              # adjust for m is odd
return f"{string[:m]}greg{string[m:]}"  # formatted string
``````
``````>>> greg_str('string')
'strgreging'
>>> greg_str('strings')
'strigregngs'
>>>
``````

Thank you so much! Your code makes so much more sense

1 Like

In case anyone else came here in/after 2023 looking for help or alternative solutions, this is what I used (with explanations) to pass the exercise! This one tripped me up for longer than it probably should have, and my code is potentially a bit long (any constructive feedback is appreciated, keeping in mind I’m a beginner to coding!), but I’m happy with my code’s structure at the moment.

``````def gregs_e_tagger(string):
# The string will be split into two sections, part_1 and part_2, so that "greg" can be put in the middle later.
part_1 = ""
part_2 = ""
if len(string) % 2 == 0:
# If the length of the string is EVEN, this is how we find the midpoint of the string, and we construct the 'gregified' string
middle = int(len(string) / 2)
part_1 += string[:middle]
part_2 += string[middle:]
gregified_string = part_1 + "greg" + part_2
else:
# If the length of the string is ODD, this is how we find the midpoint of the string, and we construct the 'gregified' string.
middle = int((len(string) / 2) - 0.5)
part_1 += string[:middle]
part_2 += string[middle:]
gregified_string = part_1 + "greg" + part_2
return gregified_string
``````