Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!