tags:
- code
topic: String
difficulty: Easy
link: https://www.codewars.com/kata/56747fd5cb988479af000028
date: 2023-12-31
Problem
You are going to be given a word. Your job is to return the middle character of the word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.
Kata.getMiddle("test") should return "es"
Kata.getMiddle("testing") should return "t"
Kata.getMiddle("middle") should return "dd"
Kata.getMiddle("A") should return "A"
def get_middle(s):
if len(s) % 2 == 0:
return s[len(s)//2-1:len(s)//2+1]
else:
return s[len(s)//2:len(s)//2+1]