Given a string of odd length greater 7, return a string made of the middle three chars of a given String

In this article, we will write a Python program - Given a string of odd length greater than 7, return a string made of the middle three chars of a given String.

Given a string of odd length greater than 7, return a string made of the middle three chars of a given String

def getMiddle(strr):
    startIndex = int(len(strr) / 2 - 1)
    endIndex = startIndex + 3
    print(strr[startIndex:endIndex])
    return


getMiddle("sourcecodeexamples")

Output:

dee

Second way:

string = 'sourcecodeexamples'
lenght = len(string)
lenght = lenght//2 -1
print(string[lenght:lenght+3])

Output:

dee

Comments