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 Python Collections (Retired) Slices sillyCase

Parvinder Grewal
Parvinder Grewal
3,678 Points

Python: How change half of a string

def sillycase(stringArg):
  lower = stringArg.lower()
  # upper: somehow upper the second half the string, how do you make that determination? 
  # I can only see ways to change a specific character.

[MOD: added ```python formatting -cf]

3 Answers

akak
akak
29,445 Points

You need to get length of the stringArg, divide it by 2 and then use that number to begin or end replacing letters with upper or lowercase.

If you're stuck, here is the code:

def sillycase(stringArg):
  length = len(stringArg)/2
  return stringArg[0:round(length)].lower() + stringArg[round(length):].upper()
Parvinder Grewal
Parvinder Grewal
3,678 Points

Thanks for your help.

Bummer! Where's sillycase()?

^^ The browser compiler returns that.

akak
akak
29,445 Points

I've updated the code, made a typo in return. Try with this one.

Parvinder Grewal
Parvinder Grewal
3,678 Points

Hey akak, I was not sure whether you original used "string" in the return statement for something else.

When you use the 0 in the first part of the return: stringArg[0:round(length).lower() > does the interpreter just know that is the first half?

Thanks.

akak
akak
29,445 Points

Yeah, I wrote string everywhere at first and did not change it to stringArg.

As for the question. stringsArg[0:round(length)] means: take string from the beginning (0 means beginning of the string) until half of the sting. We know where the half is because we took the length of the string and split in half line earlier.

So if you take word "mystring" the length of it is 8. The half of it is 4. We lower the letters from beginning till 4 and then from 4 till end of the string. And we get "mystRING".

Parvinder Grewal
Parvinder Grewal
3,678 Points

Thanks for the clarification!