Solution 1

def first_half(a_string):
    string_length = len(a_string)
    half_length = int(string_length / 2)
    if string_length % 2 == 0:
        return a_string[:half_length]
        return a_string[:half_length + 1]

First Half

The function first_half returns the "first" half of a string (pretty obvious, right?). So, if you invoke first_half('abcdef') you should get 'abc' as a result. But what happens if the string has an odd number of characters like the following example? first_half('abcXdef') (7 characters). In that case, first_half should include the character in the middle; the result should be: abcX.

For this assignment you'll have to check the length of the string and handle the even/odd cases.

Make sure you return your result!

Test Cases

test first half with even length string - Run Test

def test_first_half_with_even_length_string():
    assert first_half('abcdef') == 'abc'

test first half with odd length string - Run Test

def test_first_half_with_odd_length_string():
    assert first_half('abcXdef') == 'abcX'
def first_half(a_string): pass