← Back to challenges

Return the Middle Character(s) of a String

PythonHardstringsformattingmathregex

Instructions

Create a function that takes a string and returns the middle character(s). If the word's length is odd, return the middle character. If the word's length is even, return the middle two characters.

Examples

get_middle("test") ➞ "es"

get_middle("testing") ➞ "t"

get_middle("middle") ➞ "dd"

get_middle("A") ➞ "A"

Notes

All test cases contain a single word (as a string).

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Minimal VIII: This and That