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 trial

Python

Can you give me explanation of this code?

And also I need explanation about 'round' too

def sillycase(string):
    new_str = round(len(string) / 2)
    return  string[:new_str].lower() + string[new_str:].upper

1 Answer

Hi Chang,

The function's goal is to change the capitalization of the first half of the string to lower case, and the second half to upper case. It does this by figuring out the index of half the string using the new_str variable and calculation a rounded estimate of half the string length (round function will round to the nearest whole number). It then starts with a slice of the first half of the string:

string[:new_str] # from index 0 to half length

and converts it to lower case, and then adds a slice of the second half of the string:

string[new_str:] # from half length index to the end of the string 

and converts it to upper case