Question: write a function that returns a string containing the middle character in string if the length of string is odd, or the two middle characters
write a function that returns a string containing the middle character in string if the length of string is odd, or the two middle characters if the length is even. For example, middle(middle) retruns dd.
I completed the code but I cant use a list. How can I do this without using a list? It is commented in the code.
def middle(name):
lengthOfstr = len(name)
if lengthOfstr %2==0: #if len of strinf is even mid = int(lengthOfstr / 2) #divide length by 2 ###print( name[mid-1]+name[mid] ) mid = int(lengthOfstr/2) ###print( name[mid] )
def main(): status = True while status: input1 = str(input("Enter string (-1 to stop) :")) if input1 == '-1': status = False else: middle(input1) main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
