def trim(s): a=len(s) n=0 m=1 while n<a: if s[n] !=' ': b=n break n=n+1 while m<a: if s[-m] !=' ': c=a-m+1 break m=m+1 return s[b:c] print(trim('hello ')) print(trim(' hello')) print(trim(' hello ')) print(trim(' hello world '))
hello hello hello hello world
Sign in to make a reply
JoyElaine
def trim(s): a=len(s) n=0 m=1 while n<a: if s[n] !=' ': b=n break n=n+1 while m<a: if s[-m] !=' ': c=a-m+1 break m=m+1 return s[b:c] print(trim('hello ')) print(trim(' hello')) print(trim(' hello ')) print(trim(' hello world '))
hello hello hello hello world