Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialErik Luo
3,810 PointsHow do you split the string into first and second half?
How do you split the string into first and second half?
# The first half of the string, rounded with round(), should be lowercased.
# The second half should be uppercased.
# E.g. "Treehouse" should come back as "treeHOUSE"
def sillycase(str):
return round(str[])
1 Answer
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Pointsyou function needs to first split the string in two. The issue will be that you may have even or odd number of characters in the string. Hence, if you divide the string by two to split it, you may get a float. Therefore you are going to set the first half of the string to be the rounded value of the length of the string / 2, and the second half its total length minus this value.
once you have the lengths of the first part and last part you can take slices of the string using the lengths to get the right characters and call .upper() or .lower() on the slices. You can then return the concatenated slices.
I would write something like this:
def sillycase(string):
length_string = len(string)
first_length = round(length_string / 2)
first_half = string[0:first_length].lower()
second_half = string[first_length:].upper()
return first_half + second_half