python - Need more elegant solution to even string length -
here's task got:
given string of length, return first half. string "woohoo" yields "woo".
first_half('woohoo') → 'woo' first_half('hellothere') → 'hello' first_half('abcdef') → 'abc'
here's solution:
def first_half(str): if len(str) % 2 == 0: b = len(str) // 2 return str[:b]
my question is:
can show me simpler solution in python of course doesn't require me use variable half of string length (b)?
in python:
str = "woohoo" str = str[:-len(str)/2]
Comments
Post a Comment